C++走向远洋——67(项目二、洗牌)

*/
 * Copyright (c) 2016,烟台大学计算机与控制工程学院
 * All rights reserved.
 * 文件名:text.cpp
 * 作者:常轩
 * 微信公众号:Worldhello
 * 完成日期:2016年6月9日
 * 版本号:V1.0
 * 问题描述:洗牌
 * 程序输入:无
 * 程序输出:见运行结果
 */
#include<iostream>
#include<ctime>
#include<list>
#include<vector>
#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 Shuffled"<<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;
}


运行结果:



posted @ 2016-06-09 22:35  壹言  阅读(122)  评论(0编辑  收藏  举报