天天看點

java中異或操作 一種數出現奇數次其他出現偶數次,求這個數

package zuochengyun;

import java.util.Scanner;

public class code03 {
    /*
    * 題目描述
    * 一種數出現奇數次,其他所有數出現偶數次   int[] arr  輸出這個數
    * */

    public static void main(String[] args) {

//        int[] arr1 = { 3, 3, 2, 3, 1, 1, 1, 3, 1, 1, 1 };
        System.out.println("請輸入:");
        Scanner sc = new Scanner(System.in);
        String str=sc.nextLine().toString();
        String[] arr=str.split(" ");
        int[] b=new int[arr.length];
        //
        for (int j=0;j<b.length;j++){
            b[j]=Integer.parseInt(arr[j]);
            System.out.println(b[j]);
        }
        printOdd(b);
    }
    public static void printOdd(int [] arr) {
        int eor=0;
        for (int cur : arr){
            //異或操作
            eor ^=cur;
        }
        System.out.println(eor);
    }




}      

新建立一個公衆号 Rockey小何同學 想互相交流的同學可以關注一下哈! 感謝支援!