zoukankan      html  css  js  c++  java
  • 实现整数集合的并、交、差运算

      问题:编写一个程序,实现一个整数集合的基本运算
      要求:输入整数集合{2,4,1,3,5}和{2,5,10},输出前者的元素个数以及它们进行集合的并、交、差运算
       设计一个类IntSet,包括私有数据成员len(集合的长度)和数组s(存放集合元素),以及如下方法:
        public void insert(int d)    向集合中添加一个元素,重复的元素不能添加
        public int length()      返回集合的元素个数
        public int getInt(int i)             返回集合中位置i的元素
        public void disp()      输出集合的所有元素
        public IntSet union(IntSet s2)  实现两个集合的并运算
        public IntSet intersection(IntSet s2)  实现两个集合的交运算
        public IntSet difference(IntSet s2)  实现两个集合的差运算
        public IntSet copySet(IntSet s2)   实现两个集合的拷贝
     
    Java代码:
    public class IntSet {
           private int len;  //集合长度
           private int[] s;  //数组
           
           public IntSet() {
                  len = 0;
                  s = new int[100];
           }
        public void resize()  //重置数组
        {
            int[] b = new int[2*s.length];
            System.arraycopy(s, 0, b, 0, s.length);
            s = b;    
        }
           public void insert(int d) //集合中插入元素
           {
                 if(len>=s.length)
                 {
                     resize();
                  }
                  for(int i=0;i<len;i++) {
                         if(s[i]==d){
                               return;
                         }
                  }
              s[len]=d;
              len++;
           }
           
           public int length() //获取集合长度
           {
                  return len;
           }
           
           public int getInt(int i) {
                  
                  if(i>=0 && i<len)
                  {
                         return s[i];
                  }else {
                         return -1;
                  }
           }
           
           public void disp() {
                  for(int i=0;i<len;i++)
                  {
                         System.out.print(" "+s[i]);
                  }
           }
           
           public IntSet union(IntSet s2) //并
           {
                  int same;
                  for(int i=0;i<this.len;i++)
                  {
                         same = 0;
                         for(int j=0;j<s2.len;j++)
                         {
                               if(this.getInt(i)==s2.getInt(j))
                               {
                                      same = 1;
                                      break;
                               }
                               if(same == 0)
                               {
                                      this.insert(s2.getInt(j));
                               }
                         }
                  }
                  return this;
           }
           
           public IntSet intersection(IntSet s2) { //交
                  
                  int same;
                  for(int i=0;i<this.len;i++) {
                         same = 0;
                         for(int j=0;j<s2.len;j++) {
                               if(this.s[i] == s2.getInt(j))
                               {
                                      same = 1;  //找到重复的元素,则same=1
                                      break;
                               }
                         }
                               if(same == 0) {//找到不重复的元素,则删除
                                      for(int k=i;k<this.len;k++)
                                      {
                                             this.s[k] =this.s[k+1];
                                      }
                                      
                                      i--;
                                      this.len--;
                                      
                               }
                         
                  }
                  return this;
           }
           public IntSet difference(IntSet s2) { //差
                  
                  int same;
                  for(int i=0;i<this.len;i++) {
                         same = 0;
                         for(int j=0;j<s2.len;j++) {
                               if(this.s[i] == s2.getInt(j))
                               {
                                      same = 1; //找到重复的元素,则same=1
                                      break;
                               }
                         }
                               if(same == 1) {//找到重复的元素,则删除
                                      for(int k=i;k<this.len;k++)
                                      {
                                             this.s[k] =this.s[k+1];
                                      }
                                      
                                      i--;
                                      this.len--;
                                      
                               }
                         
                  }
                  return this;
           }
           
           public IntSet copySet(IntSet s2) {
                  this.len = s2.len;
                  for(int i=0;i<s2.len;i++) {
                         this.s[i] = s2.getInt(i);
                  }
                  return this;
           }
    }
     
    测试代码:
      public class Test {
           public static void main(String[] args) {
                  IntSet s1,s2;
                  s1 = new IntSet();
                  s2 = new IntSet();
                  s1.insert(2);
                  s1.insert(4);
                  s1.insert(1);
                  s1.insert(3);
                  s1.insert(5);
                  
                  s1.disp();
                  
                  s2.insert(2);
                  s2.insert(5);
                  s2.insert(10);
                  
                  System.out.println();
                  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();
                  
                  s2.difference(s1);
                  System.out.println();
                  s2.disp();
           }
    }
     
  • 相关阅读:
    【CodeForces 611C】New Year and Domino
    【HDU 1003】 Max Sum
    【ZOJ 3502】Contest
    【LightOJ 1422】Halloween Costumes(区间DP)
    【ZOJ 3609】Modular Inverse
    乘法逆元及其应用
    除法和取余的运算时间
    HUD3689 Infinite monkey theorem
    POJ2185 Milking Grid
    1355: [Baltic2009]Radio Transmission[循环节]
  • 原文地址:https://www.cnblogs.com/lone5wolf/p/11111937.html
Copyright © 2011-2022 走看看