#include<iostream>
#include<stdlib.h>
using namespace std;
void establishData(int a[],int length)
{
srand((int)time(NULL));
//随机产生10个数(randomly generate 10 number)
for (int i = 0; i < length - 1; ++i)
{
a[i] = rand() % 10 + 1;//产生1到10的数(produces a number from 1 to 10)
for (int j = 0; j < i; ++j)
{
if (a[j] == a[i])//排除重复的数
{
--i;
}
}
}
//在最后一个位置放入一个重复数(put a repeat number in the last number)
a[length - 1] = rand() % 10 + 1;
//将该位随机与一个不相等的数交换位置(randomly swap this bit with an unequal number)
int index = rand() % 10 + 1;
while (a[index] == a[length - 1])
{
index = rand() % 10 + 1;
}
int temp = a[index];
a[index] = a[length - 1];
a[length - 1] = temp;
}
int main()
{
int a[11];
int length = sizeof(a) / sizeof(a[0]);
establishData(a,length);
cout << "a数组11个数据" << endl;
for (int i = 0; i < length; ++i)
{
cout << a[i] << " ";
}
cout << endl;
//用^(异或)运算,0与任何数做异或运算都为该数本身
//with the^(exclusive or)operation,the exclusive or operation between 0 and any number is the number itself)
int result=0;
int i = 0;
for (int i = 1; i <= length - 1; ++i)
{
result ^= i;
}
for (; i < length; ++i)
{
result ^= a[i];
if (result == 0)
{
break;
}
}
cout << endl << endl;
cout << "结果得" << " ";
cout << a[i]<<" 重复";
}
|