本题解法:异或
异或运算的特点:两个相同的数字异或,结果为0。
因为数组中除了一个元素只出现两次之外,其它的元素都出现一次,(这里设元素1~10,其他的数值可以自己随意设置),再让这个数组中的数与1~10中的每一个数异或,别的出现一次的数异或自己后结果为0,需要找的数异或三次自己,会剩下自己,便可以最后找到这个数。
#include#include #include #define random(a,b) (rand()%(b-a)+a) using namespace std; int main(){ srand((int)time(0)); int N[11],idex; for(int i = 0;i < 10;i++){ N[i] = i + 1; } N[10] = random(1, 10); idex = random(0, 10); int t = N[10]; N[10] = N[idex]; N[idex] = t; int x1 = 0; for(int i = 1;i <= 10;i++){ x1=x1^i; } for(int i = 0;i < 11;i++){ x1=x1^N[i]; } for(int i = 0;i <= 10;i++){ cout<



