自己想的一种方法. 不知道行不行得通
要是碰巧
第1次=1
第2次=2
第3次=3
现在就随机3次了.
第1次和第3次加就=4.
第2次和第3次加就=5. 结论: 抽3次随机数.可以得5次结果. 所以.
1-10抽5个是常数级.
1-100抽50个是循环10次.
1-1百万抽五十万 循环10万次
int tmp[100万];//用来保存临时的数据
int b=1;
int n1;
int n2;
int n3;
int n4;
int n5;
int 搞不定=0;
int result[50万];
int count=0;
tmp清0;
for(;b<10万次;b++)
{
one:
n1=random(100万);
if(tmp[n1]是空)
tmp[n1]=n1; //随机的数字放到临时对应的下标
else
goto one;
two:
n2=random(100万);
if(tmp[n2]是空)
tmp[n2]=n2;
else
goto two;
three:
n3=random(100万);
if(tmp[n3]是空)
tmp[n3]=n3;
else
goto three;
if(tmp[one+three]是空)
n4=tmp[one+three]=one+three;
else if(tmp[one+tow]是空)
n4=tmp[one+tow]=one+tow;
else
搞不定=1;
if(tmp[tow+three]是空)
n5=tmp[tow+three]=tow+three;
else
搞不定=1;
if(搞不定==1)
{
tmp[n1]=tmp[n2]=tmp[n3]=tmp[one+three]=tmp[one+tow]=tmp[tow+three]=0;
b--;//当本次循环不成功
continue;
}
else //满足了,代表一次循环可以有5个不同的随机数
{
result[count]=n1;
count++;
result[count]=n2;
count++;
result[count]=n3;
count++;
result[count]=n4;
count++;
result[count]=n5;
count++;
}
}