c++指针问题报数问题
N个人围成一圈(编号分别为1,2,……,N,N<65535),从1号开始按照编号方向报数1、2、3,...n,凡报n的人离开,再从下一个人开始继续报数1、2、3..,n,报n的人又离开,反复进行此操作,请你编写程序,计算出最后留下的人的序号是多少?输入:标准输入,第一个正整数为人的数目N值,第2个数为每次报数数,仅一行输入.
输出:使用一行输出你计算的最后留下来的人的标号.
样例:
标准输入:
5 3
标准输出:
4
求各位帮帮忙。
struct node { int index; int member; node * next; }; //---------------Class chain begin--------------------- class chain { public: //constructor chain() { head = NULL; _chain_size = 0; } //destructor ~chain(){} //void create(int size); void create(int size, bool isCycleChain); void displayChain(); node *searchByIndex(int index); node *searchByGivenElement(int target); void deleteByIndex(int index,bool isChangeHead); void deleteByGivenElement(int target); void reArrangeIndexAfterDelete(); void reverseChain(); void destroy(); int getChainSize() { return _chain_size; } private: node *head; int _chain_size; }; void chain::create(int size, bool isCycleChain) { node *pCreate=NULL; node *temp = NULL; for(int i =0;i<size;++i) { pCreate = new node; if(NULL == head) { cout<<"create the first node"<<endl; head = pCreate; temp = head; head->index = i+1; cin>>head->member; pCreate=NULL; } else { //not the first //temp = temp->next; //error:" temp->next pointed to unknown address" //temp = pCreate; //error temp->next= pCreate; temp = temp->next; pCreate = NULL; temp->index = i+1; cin>>temp->member; if(size-1 == i) { //process the last node: cout<<"create the last node\n"; if(true == isCycleChain) { //if a cycle chain, the last node point to the first node: temp->next = head; } else { // a single-direction chain temp->next = NULL;//the last node's next should be NULL } } } } _chain_size = size; return; } void chain::destroy() { if(NULL == head) return; node *pDel = head; for(int i=0;i<_chain_size;i++) { head = head->next; delete pDel; pDel = head; } cout<<"destroy complete"<<endl; pDel = NULL; head = NULL; _chain_size = 0; return; } void chain::displayChain() { node *p = head; for(int i=0;i<_chain_size;i++) { cout<<"index = "<<p->index<<": "<<p->member<<endl; p = p->next; } p = NULL; return; } node *chain::searchByIndex(int index) { node *temp = head; if(temp->index == index) return temp;// when searching the first node for(int i=0;i<_chain_size;i++) { if(NULL == temp->next) { //pointer is 0, this might be an error condition, or the last node of non-cycle return temp; } if(temp->next->index == index) { //return the previous node of target, search from the second node, end with the fitst node return temp; } else { temp = temp->next; continue; } } cout<<"index = "<<index<<" is invalid "<<endl; return NULL; } void chain::deleteByIndex(int index, bool isChangeHead) { node *delOfPrevious = searchByIndex(index); node *del = NULL; if(NULL == delOfPrevious) { cout<<"target not found"<<endl; return; } del = delOfPrevious->next;//target node to be delete delOfPrevious ->next = del->next; //connect first int targetToBeDelete = del->member; if(isChangeHead) { //head will be changed head = del->next; } delete del; del = NULL; _chain_size--; cout<<"Deleted member is: "<<targetToBeDelete<<endl; return; } void chain::reArrangeIndexAfterDelete() { node *temp = head; for(int i=0;i<_chain_size;i++) { temp->index = i+1; temp = temp->next; } cout<<"index of all element updated"<<endl; return; } //---------------Class chain end----------------------- //--------------------Josef begin---------------------- void recursion_Josef(chain *pChain, int m) { if(0 == m) return; int size=pChain->getChainSize(); if(size == 1) return; if(size>=m) { pChain->deleteByIndex(m,true); } else { pChain->deleteByIndex(m%size,true); } pChain->reArrangeIndexAfterDelete(); pChain->displayChain(); //recursion recursion_Josef(pChain,m); return; } void test_Josef() { chain Chain; chain *pChain = NULL; int m,n; cout<<"input m,n respectively:"<<endl; cin>>m>>n; Chain.create(n,true); pChain = &Chain; Chain.displayChain(); recursion_Josef(pChain, m); cout<<"the last one remain is:"<<endl; pChain->displayChain(); Chain.destroy(); return; } //--------------------Josef end------------------------ //--------------------main() begin--------------------- void main() { test_Josef(); return; } //--------------------main() end-----------------------