您的位置:首页 > 其它

第十五周项目2——洗牌

2016-06-15 18:44 323 查看
问题及代码:

/*
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:main.cpp
* 作    者:赵志君
* 完成日期:2016年6月15日
* 版 本 号:v1.0
*
* 问题描述:在扑克牌游戏中,每次游戏开始都要求把54张牌重新排列一下,称为洗牌。试编写程序将一副扑克牌(用54个整数1~54表示)随机洗好后,顺序输出54张牌的情况。
*/
#include <ctime>
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
using namespace std;
typedef vector<int> IntVector;
void SwapShuffle(IntVector &datas, int time)
{
unsigned size=datas.size(),p1,p2;
while(time--)
{
p1=rand()%size;
p2=rand()%size;
swap(datas[p1],datas[p2]);
}
}
int main()
{
ostream_iterator <int>  os(cout," ");
srand(time(NULL));
vector <int> poker;
for(int i=1; i<=54; i++)
{
poker.push_back(i);
}
cout<<"洗牌前:"<<endl;
copy(poker.begin(),poker.end(),os);
cout<<endl;
SwapShuffle(poker,100);
cout<<"\n洗牌后:"<<endl;
copy(poker.begin(),poker.end(),os);
cout<<endl<<endl;
return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: