/*
*Copyright(c)2016,煙臺大學計算機與控制工程學院
*All rights reserved
*文件名稱:123.cpp
*作 者:王蕊
*完成日期:2016年6月7日
*版 本 號: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)); //函數中time是要執行交換的次數
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<<"洗牌後"<<endl;
copy(poker.begin(),poker.end(),os);
cout<<endl<<endl;
return 0;
}
運行結果: