如题,要求算法效率尽量高。
我的方案是这样的:
声明一个数组list<int> L=new list<> ();
然后将1-10一次添加到数组L中。
声明随机数random ran=new random();
for(int i=0;i<10;i++)
{
//取数
int temp=ran.next(L.count());
L[temp];
L.removeAt(temp);
}
这样只需要n次循环便可以随机生成n个不重复的数字!
如题,要求算法效率尽量高。
我的方案是这样的:
声明一个数组list<int> L=new list<> ();
然后将1-10一次添加到数组L中。
声明随机数random ran=new random();
for(int i=0;i<10;i++)
{
//取数
int temp=ran.next(L.count());
L[temp];
L.removeAt(temp);
}
这样只需要n次循环便可以随机生成n个不重复的数字!