实现整数集合的并、交、差运算

怼烎@ 2021-11-16 09:04 400阅读 0赞

问题:编写一个程序,实现一个整数集合的基本运算

要求:输入整数集合{2,4,1,3,5}和{2,5,10},输出前者的元素个数以及它们进行集合的并、交、差运算

设计一个类IntSet,包括私有数据成员len(集合的长度)和数组s(存放集合元素),以及如下方法:

  1. public void insert(int d) 向集合中添加一个元素,重复的元素不能添加
  2. public int length() 返回集合的元素个数
  3. public int getInt(int i) 返回集合中位置i的元素
  4. public void disp() 输出集合的所有元素
  5. public IntSet union(IntSet s2) 实现两个集合的并运算
  6. public IntSet intersection(IntSet s2) 实现两个集合的交运算
  7. public IntSet difference(IntSet s2) 实现两个集合的差运算
  8. public IntSet copySet(IntSet s2) 实现两个集合的拷贝

Java代码:

public class IntSet {

  1. private int len; //集合长度
  2. private int\[\] s; //数组
  3. public IntSet() \{
  4. len = 0;
  5. s = new int\[100\];
  6. \}
  7. public void resize() //重置数组
  8. \{
  9. int\[\] b = new int\[2\*s.length\];
  10. System.arraycopy(s, 0, b, 0, s.length);
  11. s = b;
  12. \}
  13. public void insert(int d) //集合中插入元素
  14. \{
  15. if(len>=s.length)
  16. \{
  17. resize();
  18. \}
  19. for(int i=0;i<len;i++) \{
  20. if(s\[i\]==d)\{
  21. return;
  22. \}
  23. \}
  24. s\[len\]=d;
  25. len++;
  26. \}
  27. public int length() //获取集合长度
  28. \{
  29. return len;
  30. \}
  31. public int getInt(int i) \{
  32. if(i>=0 && i<len)
  33. \{
  34. return s\[i\];
  35. \}else \{
  36. return -1;
  37. \}
  38. \}
  39. public void disp() \{
  40. for(int i=0;i<len;i++)
  41. \{
  42. System.out.print("\\t"+s\[i\]);
  43. \}
  44. \}
  45. public IntSet union(IntSet s2) //并
  46. \{
  47. int same;
  48. for(int i=0;i<this.len;i++)
  49. \{
  50. same = 0;
  51. for(int j=0;j<s2.len;j++)
  52. \{
  53. if(this.getInt(i)==s2.getInt(j))
  54. \{
  55. same = 1;
  56. break;
  57. \}
  58. if(same == 0)
  59. \{
  60. this.insert(s2.getInt(j));
  61. \}
  62. \}
  63. \}
  64. return this;
  65. \}
  66. public IntSet intersection(IntSet s2) \{ //交
  67. int same;
  68. for(int i=0;i<this.len;i++) \{
  69. same = 0;
  70. for(int j=0;j<s2.len;j++) \{
  71. if(this.s\[i\] == s2.getInt(j))
  72. \{
  73. same = 1; //找到重复的元素,则same=1
  74. break;
  75. \}
  76. \}
  77. if(same == 0) \{//找到不重复的元素,则删除
  78. for(int k=i;k<this.len;k++)
  79. \{
  80. this.s\[k\] =this.s\[k+1\];
  81. \}
  82. i--;
  83. this.len--;
  84. \}
  85. \}
  86. return this;
  87. \}
  88. public IntSet difference(IntSet s2) \{ //差
  89. int same;
  90. for(int i=0;i<this.len;i++) \{
  91. same = 0;
  92. for(int j=0;j<s2.len;j++) \{
  93. if(this.s\[i\] == s2.getInt(j))
  94. \{
  95. same = 1; //找到重复的元素,则same=1
  96. break;
  97. \}
  98. \}
  99. if(same == 1) \{//找到重复的元素,则删除
  100. for(int k=i;k<this.len;k++)
  101. \{
  102. this.s\[k\] =this.s\[k+1\];
  103. \}
  104. i--;
  105. this.len--;
  106. \}
  107. \}
  108. return this;
  109. \}
  110. public IntSet copySet(IntSet s2) \{
  111. this.len = s2.len;
  112. for(int i=0;i<s2.len;i++) \{
  113. this.s\[i\] = s2.getInt(i);
  114. \}
  115. return this;
  116. \}

}

测试代码:

public class Test {

  1. public static void main(String\[\] args) \{
  2. IntSet s1,s2;
  3. s1 = new IntSet();
  4. s2 = new IntSet();
  5. s1.insert(2);
  6. s1.insert(4);
  7. s1.insert(1);
  8. s1.insert(3);
  9. s1.insert(5);
  10. s1.disp();
  11. s2.insert(2);
  12. s2.insert(5);
  13. s2.insert(10);
  14. System.out.println();
  15. s2.disp();

// s1.union(s2);

// System.out.println();

// s1.disp();

// s1.intersection(s2);

// System.out.println();

// s1.disp();

// s1.difference(s2);

// System.out.println();

// s1.disp();

// IntSet s6 = new IntSet();

// s6.copySet(s2);

// s6.disp();

  1. s2.difference(s1);
  2. System.out.println();
  3. s2.disp();
  4. \}

}

转载于:https://www.cnblogs.com/lone5wolf/p/11111937.html

发表评论

表情:
评论列表 (有 0 条评论,400人围观)

还没有评论,来说两句吧...

相关阅读

    相关 整数运算

    > 一般数据范围: > int型:10^9 > long long型:10^18 > double型:10^308 > 如果要存储的数据比这些还大,就需要使用大

    相关 实现集合运算

    ![70][] 如图,2棵树表示2个集合,用一个数组存储多棵树 注意:(1).用树表示集合,树的每一个节点代表集合中的一个元素,并且从上到下,从左到右放从小到大的元素 (