一个数组中有一个重复的数字,时间复杂度O(n),空间复杂度O(1) 找出来
#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n = 1001;
int *a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = i+1;
}
a[n-1] = rand()%n;
for (int i = 0; i < n; i++)
{
cout<<a[i]<<" ";
}
cout << endl;
int x = 0;
for (int i = 1; i <= n-1; i++)
{
x = (x ^ i);
}
for (int i = 0; i < n; i++)
{
x = x ^ a[i];
}
cout <<"重复的元素为:"<< x << endl;
return 0;
}