洗牌
2016-06-21 13:16
369 查看
/*
*copyright(c) 2014,烟台大学计算机学院
*All rights reserved
*文件名称:test.cpp
*作者:吴雨凡
*版本:v6.0
*
*问题描述:洗牌
*输入描述:
*程序输出:判断相关问题
*/
#include <ctime>
#include <vector>
#include <list>
#include <iostream>
#include <iterator>
#include <cstdlib>
using namespace std;
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 Shuffle"<<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;
}
*copyright(c) 2014,烟台大学计算机学院
*All rights reserved
*文件名称:test.cpp
*作者:吴雨凡
*版本:v6.0
*
*问题描述:洗牌
*输入描述:
*程序输出:判断相关问题
*/
#include <ctime>
#include <vector>
#include <list>
#include <iostream>
#include <iterator>
#include <cstdlib>
using namespace std;
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 Shuffle"<<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;
}
相关文章推荐
- Hibernate之Session merge与update方法
- 线性模型学习笔记
- Nginx下Redmine配置
- Magento前端开发指南(二)
- android ListView详解,你不知道的事 (后附焦点解决方法)
- Swift - 各种手势检测大全(UIGestureRecognizer及其子类)
- MNIST 可视化
- EF--Codefirst 加密数据库连接字符串
- VS code golang 开发环境搭建
- Android 开源框架 反编译工具
- ACPI 主要Table作用
- linux用户及管理权限
- textfiled值得拥有
- Linux开发环境搭建之Samba服务器配置
- 如何将一个项目导出成API文档
- 图结构练习——BFS——从起始点到目标点的最短步数
- 使用RecyclerView实现滑动添加、滑动删除,以及瀑布流、ListView、GridView的转换
- bootstrapTable跨域问题
- windows下安装rabbitmq的php扩展amqp
- Qt: QFile类读写文本实例