现在有一个长度为 n 的正整数序列,其中只有一种数值出现了奇数次,其他数值均出现偶数次,请你找出那个出现奇数次的数值。
数据范围:
n = int(input()) res = list(map(int, input().split())) num = 0 for i in res: num = num^i print(num)
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String strN; while((strN = br.readLine()) != null) { String[] strArr = br.readLine().split(" "); int remain = 0; for(int i = 0; i < strArr.length; i++) // 出现偶数次的数在经过不断异或后一定为0,与0异或不改变数 remain ^= Integer.parseInt(strArr[i]); // 所以最后剩下的数就是出现过奇数次的数 System.out.println(remain); } } }
#include<stdio.h> int main() { int n=0; int num=0; int sum=0; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&num); sum^=num; } printf("%d\n",sum); return 0; }
//这玩意儿是看运气的,我运行了3次,前两次超时了,第三次通过了,f*ck!!! #include <iostream> #include <string> #include <vector> #include <algorithm> #include <limits.h> #include <stack> #include <unordered_map> #include <map> #include <queue> using namespace std; void findTheNumberAppearOnlyOnce() { int n, x, res = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> x; res ^= x; } cout << res << endl; } int main(){ findTheNumberAppearOnlyOnce(); return 0; }
n = int(input().strip()) array = list( map(int, input().strip().split()) ) ans = 0 for a in array: ans ^= a print(ans)
// 40%超时了 import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); Map<String, Integer> map = new HashMap(); for(int i = 0; i < n; i++){ if(!map.containsKey(str[i])){ map.put(str[i], 1); }else{ map.put(str[i], map.get(str[i])+1); } } for(String key:map.keySet()){ if(map.get(key)%2!=0){ System.out.println(key); return; } } } }