c++如何对N个数字随机排序后并输出

来源:百度知道 编辑:UC知道 时间:2024/05/30 13:20:51
如何编写可以实现如下功能的C++程序源代码:
数组:0 1 2 3 4 5 6 7 8 9
怎么排序成随机序列?
使用了什么排序方法?

#include<vector>
#include<algorithm>//random_shuffle()

using namespace std;
...// some operation

vector<int> data;
...//input your data;

random_shuffle( data.begin(), data.end() ); //shuffle your data

...//other operation

int a[10]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

srand(time(0));
for (int i=9; i>0; --i)
{
int idx = rand()%i;
if (idx == i) continue;
int tmp=a[idx];
a[idx] = a[i];
a[i] = tmp;
}

你是要求把一组数据转化为随机存储的格式吧。
可以采用随机数选数组下标法。
相必这个你会吧。