1、数据数据 结结 构构 课课 程程 设计题设计题 目目(C+)刘新刘新 320130938311题目题目 1:猴子选王(学时:猴子选王(学时: 3)一堆猴子都有编号,编号是 1,2,3 .m,这群猴子(m 个)按照 1-m的顺序围坐一圈,从第 1开始数,每数到第 n个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。要求:m 及 n要求从键盘输入,存储方式采用向量及链表两种方式实现该问题求解。 /*Data Structure Experiment - Project 1Author: Bird Liu (Liu Xin)Created Date: 2015-03-
2、08*/#include #include #include using namespace std;/* Class Definition */class Monkey public:Monkey() flag = 1;Monkey(int num) initNum = num; flag = 1;void setInitNum(int num);void setTempNum(int num);void setFlag(int num);int getInitNum();int getTempNum();int getFlag();Monkey* next;Monkey* last;pri
3、vate:int initNum, tempNum;int flag;/* Class Functions Definition*/void Monkey:setInitNum(int num) initNum = num;void Monkey:setTempNum(int num) tempNum = num;void Monkey:setFlag(int num) flag = num;int Monkey:getInitNum() return initNum;int Monkey:getTempNum() return tempNum;int Monkey:getFlag() ret
4、urn flag;/* Vector Function */int runWithVector() /* Initilize Monkey Group */int m;cout m;if (m monkeyCircle;vector :iterator iter;for (int i = 0; i n; leftCount = m;if (n = n) monkeyCircle.erase(monkeyCircle.begin() + n - 1);leftCount-;else n = n - leftCount - 1;monkeyCircle.erase(monkeyCircle.beg
5、in() + n);leftCount -;if (leftCount = 1)cout m n;if (m next = first;Monkey* p = first;for(int i = 1; i setInitNum(i);Monkey* newMonkey = new Monkey;p-next = newMonkey;p = p-next;else p-setInitNum(i);p-next = first;/* Finder */Monkey* temp = head;Monkey* temp2 = new Monkey;while(leftCount != 1) for (
6、int i = 1; i next;if (temp-next != head-next) temp2 = (temp-next)-next;free(temp-next);temp-next = temp2;else head-next = temp;leftCount-;temp = head;cout next)-getInitNum() choose;if (choose = 1)runWithVector();elseif (choose = 2)runWithChain();elsecerr using namespace std;struct Nodechar c;Node *next;Node *last;int num;int main() char array100;Node *head = new Node;Node *pBottom = new Node;head-next = NULL;head-last = NULL;Node *p = head;cin array;for (int i = 0; i c = arrayi;pNew-num = i;pNew-next = NULL;pNew-last = p;p-next = pNew;p = pNew;