Topic
描述: 详细描述:N个人围坐在一个圆桌上,顺时针报数,报数的初始值为第一个人设置。当有成员报出的数字为7的倍数或数字中包含7,则该人退出圆桌,而后由下一个人开始重新继续该游戏。实现以下接口: 1、设定输入原始的圆桌游戏的人数。以最先开始报数的人编号为1,顺时针排序。 2、设定第一个人的初始值,获取按照规则退出圆桌的人的编号。 3、结束游戏。样例:比如初始化为4人的游戏:第1轮初始值为1,退出为3;第2轮初始值为4,退出为4号;第三轮初始值为16,退出为2号;第四轮初始值为6,退出为1号;练习阶段: 中级
Code
/* ---------------------------------------* Date: 2015-06-31* sjf0115* title: Roundtable game * Source: Huawei Machine Test Exercises----------------------- ------------------*/#include <iostream>#include <list>#include "oj.h"using namespace STD;//Number of playersintPlayernum =0;//Round Table list<int>Circle//Start player list<int>:: Iterator current;//function: Set the number of players//input: Unsigned int nplaynum number of players 0 < Nplaynum < 10000;//output: None//return: NonevoidSetplayernum (unsigned intNplaynum) {playernum = Nplaynum;//Initial numbering for(inti =0; i < playernum;++i) {Circle.push_back (i+1); }//for //Start player by defaultCurrent = Circle.begin ();}//To determine whether or not to be kicked outBOOLIsout (intNUM) {multiples of//7 if(num%7==0){return true; }//if //contains 7 while(num) {if(num%Ten==7){return true; }//ifNum/=Ten; }//while}//function: According to the value of Ninitialnum, get the number of the person under the rule under the table//input: Unsigned int ninitialnum The initial value of this round 0 < Ninitialnum < 10000;//output: None//return: The number of the person who withdrew from the round Tableunsigned intGetplayerout (unsigned intNinitialnum) {intn = ninitialnum;intindex =0; while(Playernum >=1){if(Isout (Ninitialnum)) {--playernum; list<int>:: Iterator next = ++current;if(Next = Circle.end ()) {next = Circle.begin (); }//if--current;intOutnum = *current; Circle.erase (current); current = next;returnOutnum; }//if++ninitialnum; ++current;if(Current = = Circle.end ()) {current = Circle.begin (); }//if}//while return 0;}//function: Game over, release resources//input: None//output: None//return: NonevoidGameover () {circle.clear (); Playernum =0; current = NULL;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Huawei Machine Test exercises]25. Round Table Game