/** Assume we are using the first understanding of the problem:
"我们理解的是放1~N中的N-1个数,哪一个数缺失哪一个数重复不确定."
Then see bugs for different algorithms below.
All algorithms using sums of numbers are subject to overflow problem: assuming
N = 1e9.
*/
#include <iostream>
#include "hjns.h"
using namespace std;
/** bug for lzyssy's algo:
20 9 11 10 16 12 6 2 1 14 5 17 19 8 18 4 3 15 13 7
20 9 11 10 16 12 6 2 1 14 5 17 19 8 18 4 3 15 7 7
重复的数字为 0
重复的数字为 7
Press any key to continue . . .
16 12 8 14 4 7 11 20 1 3 5 18 15 6 19 2 17 10 13 9
16 12 8 14 4 7 11 20 1 3 5 18 15 6 19 9 17 10 13 9
重复的数字为 7
重复的数字为 9
Press any key to continue . . .
*/
void search(int N, int a[])
{
int i,flag;
for(i=0;i<N;i++)
{
flag=a[i]%N;
if(a[flag]<N)
a[flag]+=N;
else
printf("重复的数字为%3d\n",flag);
}
}
/** bug for lishizelibin's algo:
crashed for input:
4 7 12 8 14 3 6 5 19 17 2 1 15 16 20 10 11 18 9 13
4 7 12 13 14 3 6 5 19 17 2 1 15 16 20 10 11 18 9 13
20 6 9 15 7 5 11 16 19 2 10 17 4 8 12 18 14 13 3 1
20 6 9 15 7 5 11 16 19 2 10 17 4 8 12 18 14 13 1 1
*/
int do_dup(int a[],int N)
{
int temp;
while (a[0]!=a[a[0]])
{
temp = a[0];
a[0] = a[temp];
a[temp] = temp;
}
return a[0];
}
int main()
{
const int kSize = 20;
int a[kSize];
int rndIdx;
hj::number::randPerm(a, kSize);
hj::number::random rd;
hj::print(a, kSize);
rndIdx = rd(0, kSize-2);
a[rndIdx] = a[kSize-1];
hj::print(a, kSize);
search(kSize, a);
//printf("%d\n", do_dup(a, kSize));
return 0;
}