您的位置:首页 > 其它

第十五周项目二洗牌二

2016-06-24 12:40 363 查看
/*
*Copyright(c)2016,烟台大学计算机与控制工程学院
*All right reserved.
*文件名称:main.cpp
*作    者:闫舒
*完成日期:2016年6月24日
*版 本 号:vc++6.0
*
*问题描述:随机交换两个位置的元素来洗牌。
*输入描述:
*程序输出:
*/
#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<<"Before Shuffle"<<endl;
copy(poker.begin(),poker.end(),os);
cout<<endl;
SwapShuffle(poker,100);
cout<<"\nAfter Shuffled"<<endl;
copy(poker.begin(),poker.end(),os);
cout<<endl<<endl;
return 0;
}

运行结果:



注:

函数中time是要执行交换的次数,如果是54张牌的话,交换次数大于27的话就已经表现出很随机的排列了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: