欢迎来到天天文库
浏览记录
ID:29769811
大小:238.00 KB
页数:64页
时间:2018-12-23
《《数据结构》课程设计题目》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、《数据结构》课程设计题目(C++)刘新320130938311题目1:猴子选王(学时:3)一堆猴子都有编号,编号是1,2,3...m,这群猴子(m个)按照1-m的顺序围坐一圈,从第1开始数,每数到第n个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。要求:m及n要求从键盘输入,存储方式采用向量及链表两种方式实现该问题求解。/*DataStructureExperiment-Project1Author:BirdLiu(LiuXin)CreatedDate:2015-03-08*/#include#include<
2、cstdlib>#includeusingnamespacestd;/*ClassDefinition*/classMonkey{public:Monkey(){flag=1;};Monkey(intnum){initNum=num;flag=1;};voidsetInitNum(intnum);voidsetTempNum(intnum);voidsetFlag(intnum);intgetInitNum();intgetTempNum();intgetFlag();Monkey*next;Monkey*last;private:intinitN
3、um,tempNum;intflag;};/*ClassFunctionsDefinition*/voidMonkey::setInitNum(intnum){initNum=num;}voidMonkey::setTempNum(intnum){tempNum=num;}voidMonkey::setFlag(intnum){flag=num;}intMonkey::getInitNum(){returninitNum;}intMonkey::getTempNum(){returntempNum;}intMonkey::getFlag(){returnflag;
4、}/*VectorFunction*/intrunWithVector(){/*InitilizeMonkeyGroup*/intm;cout<<"Pleaseinputm:";cin>>m;if(m<3){cerr<<"Notallowed!"<monkeyCircle;vector::iteratoriter;for(inti=0;i5、+){monkeyCircle.push_back(monkeyGroup[i]);}/*Processing*/intn,leftCount;cout<<"Pleaseinputn:";cin>>n;leftCount=m;if(n<1){cerr<<"Notallowed!"<=n){monkeyCircle.erase(monkeyCircle.begin()+n-1);leftCount--;}else{n=n-leftCount-1;monkeyCircle.erase(monke6、yCircle.begin()+n);leftCount--;}if(leftCount==1){cout<<"TheKingisNo."<>m>>n;if(m<3){cerr<<"Notallowed!"<7、=m;/*GenerateChain*/Monkey*head=newMonkey;Monkey*first=newMonkey;head->next=first;Monkey*p=first;for(inti=1;i<=m;i++){if(i!=m){p->setInitNum(i);Monkey*newMonkey=newMonkey;p->next=newMonkey;p=p->next;}else{p->setInitNum(i);p->next=first;}}/*Finder*/Monkey*temp=head;Monkey*temp2=newMonk8、ey;wh
5、+){monkeyCircle.push_back(monkeyGroup[i]);}/*Processing*/intn,leftCount;cout<<"Pleaseinputn:";cin>>n;leftCount=m;if(n<1){cerr<<"Notallowed!"<=n){monkeyCircle.erase(monkeyCircle.begin()+n-1);leftCount--;}else{n=n-leftCount-1;monkeyCircle.erase(monke
6、yCircle.begin()+n);leftCount--;}if(leftCount==1){cout<<"TheKingisNo."<>m>>n;if(m<3){cerr<<"Notallowed!"<7、=m;/*GenerateChain*/Monkey*head=newMonkey;Monkey*first=newMonkey;head->next=first;Monkey*p=first;for(inti=1;i<=m;i++){if(i!=m){p->setInitNum(i);Monkey*newMonkey=newMonkey;p->next=newMonkey;p=p->next;}else{p->setInitNum(i);p->next=first;}}/*Finder*/Monkey*temp=head;Monkey*temp2=newMonk8、ey;wh
7、=m;/*GenerateChain*/Monkey*head=newMonkey;Monkey*first=newMonkey;head->next=first;Monkey*p=first;for(inti=1;i<=m;i++){if(i!=m){p->setInitNum(i);Monkey*newMonkey=newMonkey;p->next=newMonkey;p=p->next;}else{p->setInitNum(i);p->next=first;}}/*Finder*/Monkey*temp=head;Monkey*temp2=newMonk
8、ey;wh
此文档下载收益归作者所有