描述
给出一个数组,数组中的数字皆为正整数,除了某一个数字,其他的数字都会出现三次。 找出那个只出现一次的数。
输入
3n+1的正整数数组,使用逗号(,)分隔(n>0)
输出
单独出现的数字
输入样例
2,3,2,2
5,1,4,5,4,5,4
输出样例
3
1
题解:详解:https://blog.csdn.net/baoendemao/article/details/39925679
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int Find(int a[], int n){
int bits[32];
memset(bits, 0, sizeof bits);
for(int i = 0; i < n; i++)
for(int j = 0; j < 32; j++)
bits[j] += ((a[i]>>j)&1);
int res = 0;
for(int j = 0; j < 32; j++)
if(bits[j]%3 != 0)
res += (1<<j);
return res;
}
int main()
{
char line[1000001];
while (cin.getline(line, 1000000)) {
int x = 0, id = 0;
char *p = strtok(line, ",");
while(p){
sscanf(p, "%d", &x);
a[id++] = x;
p = strtok(NULL,",");
}
cout << Find(a, id) << endl;
}
return 0;
}