洗牌(1)

来源:互联网 发布:近年来赫哲族人口数据 编辑:程序博客网 时间:2024/05/05 09:43

问题及描述;

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称;test.cpp *作者;邱凯 *完成日期;2016年6月6号 *版本号;v6.0 *问题描述;    洗牌(1) *输入描述;    输入答案 *输出描述;    输出答案*/#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 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;}

运行结果;


0 0