日期:2014-05-20  浏览次数:20719 次

数组的计数
已知两个整型数组A和B,A的长度是100,数组内储存了100个不重复的整数,数组B的长度是99,从数组A内随机取出99个不重复的整数,储存在数级B内,求:在数组A内,但不在数组B内的那个整数

------解决方案--------------------
Java code

public static void main(String[] args) {
        List<Integer> A = new ArrayList<Integer>();
        List<Integer> B = new ArrayList<Integer>();
        for (int i = 0; i < 100; i++) {
            A.add(i);
            if(i == 99){
                break;
            }
            B.add(i);
        }
        
        for (int i = 0; i < B.size(); i++) {
            A.remove(B.get(i));
        }
        int result = A.get(0);
        System.out.println(result);
        
    }

------解决方案--------------------
Java code

public class Demo {
    
    private  int[] A = new int[100];
    private  int[] B = new int[99];
    private int random =0;
    
    private void initA(){
        for(int i = 0; i < 100; i++)
            A[i] = i;
    }
    
    private void getBFromA(){
        Random r=new Random();
        random = r.nextInt(99);
        for(int i = 0; i < random; i++)
            B[i] = A[i];
        for(int j = random; j < 99; j++)
            B[j] = A[j+1];
    }
    
    public static void main(String[] args) {
        Demo d = new Demo();
        d.initA();
        d.getBFromA();
        for(int i:d.A)
            System.out.print(i + " ");
        System.out.println();
        for(int i:d.B)
            System.out.print(i + " ");
        System.out.println();
        System.out.println("在A不在B:"+d.A[d.random]);
    }
}