zoukankan      html  css  js  c++  java
  • 找出数组中没有重复值的两个值

    public static void main(String[] args) {
            int[] arr = {15,1,15,16,1,15,16,13};
            ArrayList<Integer> arr1 = new ArrayList();
            ArrayList<Integer> arr2 = new ArrayList();
            int result = 0;
            for (int i = 0; i < arr.length; i++) {
                result = result ^ arr[i];
            }
            System.out.println(result);
    
            //分两个数组
            for (int i = 0; i < arr.length; i++) {
                if (isIntNumberNBitONEInBinary(arr[i],2)){
                    arr1.add(arr[i]);
                }else{
                    arr2.add(arr[i]);
                }
            }
            for (int i = 0; i < arr1.size(); i++) {
                System.out.print(arr1.get(i) + " ");
            }
            System.out.println("");
            System.out.println("--------");
            for (int i = 0; i < arr2.size(); i++) {
                System.out.print(arr2.get(i) + " ");
            }
            System.out.println("");
            System.out.println("--------");
            result = 0;
            for (int i = 0; i < arr1.size(); i++) {
                result = result ^ arr1.get(i);
            }
            System.out.println(result);
            result = 0;
            for (int i = 0; i < arr2.size(); i++) {
                result = result ^ arr2.get(i);
            }
            System.out.println(result);
    //        SpringApplication.run(PrintApplication.class, args);
        }
    
        private static boolean isIntNumberNBitONEInBinary(int number,int nbit){
            boolean result = false;
            if((number%(Math.pow(2, nbit)))/(Math.pow(2, nbit-1)) >= 1.0){
                result = true;
            }
            return result;
        }

    一个数组中两个值是单独存在的,其余的值都是重复的两个值。

  • 相关阅读:
    4月21日Java作业
    5.14 Java作业
    第十周java作业
    4月30号作业
    第七周上机
    4.9Java
    通宵看剧有感
    error: pathspec 'xxxxxxxxx' did not match any file(s) known to git
    markdown格式测试
    博客申请通过啦
  • 原文地址:https://www.cnblogs.com/wongem/p/10280936.html
Copyright © 2011-2022 走看看