关于c++中的随机排序问题

我需要做一个随机排序的问题,要求是随机列出几个数组,然后把3-20这18个数随机排进这几个数组中。请高手帮忙,最好可以有代码,没有的话意见也行

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <ctime>
using namespace std;

typedef vector<int*> ContainerType1;

const int MAX_ARRAY_NUMBER = 10; // 最多生成10个数组
const int ARRAY_SIZE = 18;

class Delete
{
public:
template <typename T>
void operator ()(T *t)
{
delete t;
}
};

template <typename T>
inline bool Find(T *t1, T *t2, const T &t)
{
if(t1 > t2)
{
T *temp = t1;
t1 = t2;
t2 = temp;
}

for(const T *p = t1; p != t2; ++p)
{
if(*p == t)
{
return true;
}
}

return false;
}

int main()
{
srand(time(NULL));

// 随机列出几个数组()
ContainerType1 CT;
int iNumber = rand() % MAX_ARRAY_NUMBER;
for(int i = 0; i < iNumber; ++i)
{
CT.push_back(new int[ARRAY_SIZE]);
memset(CT[i], 0, ARRAY_SIZE * sizeof(int));
}
cout << "一共生成了 " << iNumber << " 个, 长度为 " << ARRAY_SIZE << " 的数组" << endl;

// 随机数填充
int iRandom = 0;
for(int i = 0; i < CT.size(); ++i)
{
for(int j = 0; j < ARRAY_SIZE; ++j)
{
do
{
iRandom = rand() % 18 + 3;
}
while(Find(&CT[i][0], &CT[i][j], iRandom) == true);

CT[i][j] = iRandom;
}
}

// 显示
for(int i = 0; i < CT.size(); ++i)
{
copy(&CT[i][0], &CT[i][ARRAY_SIZE], ostream_iterator<int>(cout, " "));
cout << endl;
}

// 回收资源
for_each(CT.begin(), CT.end(), Delete());

return 0;
}

// 如果是vc6编译器,请对重名的循环变量做更名处理
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-09-22
随机列出几个数组是什么意思?你的意思就是把3-20随机排列吧?
第2个回答  2009-09-22
二维指针。
相似回答