<pre name="code" class="cpp">*文件名称:test.cpp
*作 者:陈文浩
*完成日期:2016年6月11号
*/
在扑克牌游戏中,每次游戏开始都要求把54张牌重新排列一下,称为洗牌。试编写程序将一副扑克牌(用54个整数1~54表示)随机洗好后,顺序输出54张牌的情况。
#include <ctime>
#include <vector>
#include <list>
#include <iostream>
#include <iterator>
#include <cstdlib>
using namespace std;
typedef list<int> IntList;
typedef unsigned int VIndex;
void listShuffle(IntList &unshuffled,IntList &shuffled)
{
VIndex p, size=unshuffled.size();
IntList::iterator iter;
while(size)
{
p=rand()%size--;
iter=unshuffled.begin();
while(p!=0)
{
iter++;
p--;
}
shuffled.push_back(*iter);
unshuffled.erase(iter);
}
}
int main()
{
ostream_iterator<int> os(cout," ");
srand(time(NULL));
IntList cl,scl;
for(VIndex i=1; i<=54; i++)
{
cl.push_back(i);
}
cout<<"Before Shuffle"<<endl;
copy(cl.begin(),cl.end(),os);
cout<<endl;
listShuffle(cl,scl);
cout<<"\nAfter Shuffled"<<endl;
copy(scl.begin(),scl.end(),os);
cout<<endl<<endl;
return 0;
}