*Copyright(c) 2016.烟台大学计算机与控制工程学院
*ALL rights reserved.
*文件名称:main.cpp
*作者:孙亚茹
*完成日期:2016年6月16日
*问题描述:将一副牌随机洗好后,顺序输出54张牌的情况。
*//
#include <ctime>
#include <vector>
#include <list>
#include <iostream>
#include <iterator>
#include <cstdlib>
using namespace std;
typedef vector<int> IntVector;
typedef unsigned int VIndex;
void vectorShuffle(IntVector &unshuffled,IntVector &shuffled)
{
VIndex p,size=unshuffled.size();
while(size)
{
p=rand()%size--;
shuffled.push_back(unshuffled[p]);
unshuffled.erase(unshuffled.begin()+p);
}
}
int main()
{
ostream_iterator<int> os(cout," ");
srand(time(NULL));
IntVector c,sc;
for(VIndex i=1; i<=54; i++)
{
c.push_back(i);
}
cout<<"Before Shuffle"<<endl;
copy(c.begin(),c.end(),os);
cout<<endl;
vectorShuffle(c,sc);
cout<<"\nAfter Shuffled"<<endl;
copy(sc.begin(),sc.end(),os);
cout<<endl<<endl;
return 0;
}
总结:
参考老师的答案,随机排数,可以按照顺序将数存入vector中,然后从容器中随机取出一个个数放入另一个vector数组中,重复54次,也可以用list。