有N个人围成一圈做游戏,规则如下:从某一个人开始报数,依次报1,2,3,喊到3的人出局。
2017-11-25 16:22
393 查看
有N 个人围成一圈做游戏,规则如下: 从某一个人开始报数,依次报1,2,3,喊到3的人出局。 下一个人接着从1开始报数,直到最后只剩下一个人。 问最后剩下的是谁?
#include <iostream> using namespace std; int who_win (const int& num_of_players); int main() { int num; cin>>num; int ret=who_win(num); cout<<ret<<endl; return 0; } int who_win(const int& num_of_players) { int* array=new int[num_of_players]; int* p=array; for( int i=0;i<num_of_players;i++) *(p+i)=i+1; int i=0; int index=0; int out_person=0; while(out_person+1<num_of_players) { if( *(p+i) ) index++; if(index==3) { index=0; *(p+i)=0; out_person++; } i++; if(i==num_of_players) i=0; } while(!*p) { p++; } int ret=*p; delete []array; return ret; }
或者用环形链表:
#ifndef JOSEFU_H_INCLUDED #define JOSEFU_H_INCLUDED #include <iostream> #include<stdio.h> using namespace std; typedef struct node { int data; struct node* next; }node,*pnode; pnode addback(pnode phead,int data); pnode addfront(pnode phead,int data); pnode deletefirst(pnode phead,int data); pnode pdeletefirst(pnode phead,int data, pnode* p); pnode finedata(pnode phead,int data); pnode insertfirst(pnode phead,int finedata, int data); pnode insertfirstback(pnode phead,int finedata, int data); int getnum(pnode phead); void showall(pnode phead); #endif // JOSEFU_H_INCLUDED
#include "Josefu.h" pnode addback(pnode phead,int data) { pnode pnew=new node; pnew->data=data; if(phead==NULL) { phead=pnew; pnew->next=phead; } else { pnode p=phead; while(p->next!=phead) { p=p->next; } p->next=pnew; pnew->next=phead; } return phead; } pnode addfront(pnode phead,int data) { pnode pnew=new node; pnew->data=data; if(phead==NULL) { phead=pnew; pnew->next=phead; } else { pnode p=phead; while(p->next!=phead) { p=p->next; } p->next=pnew; pnew->next=phead; phead=pnew; } return phead; } pnode deletefirst(pnode phead,int data) { if(phead->next==phead)//判断只有一个节点的情况 { if(phead->data==data) { delete phead; return NULL; } } else { pnode pre=NULL; pnode cur=phead; while(cur->next!=phead)//判断第一个节点到倒二个节点是否有相等值 { if(cur->data==data) { if(pre==NULL) { while(cur->next!=phead) { cur=cur->next; } pnode r=phead; phead=phead->next; cur->next=phead; delete r; } else { pre->next=cur->next; delete cur; } return phead; } pre=cur; cur=cur->next; } if(cur->data==data)//判断最后一个节点 { pre->next=phead; delete cur; return phead; } } } pnode pdeletefirst(pnode phead,int data, pnode* p) { getnum(phead); if(phead->next==phead)//判断只有一个节点的情况 { if(phead->data==data) { delete phead; *p=NULL; return NULL; } } else { pnode pre=NULL; pnode cur=phead; while(cur->next!=phead)//判断第一个节点到倒二个节点是否有相等值 { if(cur->data==data) { if(pre==NULL) { while(cur->next!=phead) { cur=cur->next; } pnode r=phead; phead=phead->next; cur->next=phead; delete r; *p=phead; } else { pre->next=cur->next; *p=cur->next; delete cur; } return phead; } pre=cur; cur=cur->next; } if(cur->data==data)//判断最后一个节点 { pre->next=phead; delete cur; *p=phead; return phead; } } } pnode finedata(pnode phead,int data) { pnode p=phead; while(p->next!=phead) { if(p->data==data) return p; p=p->next; } if(p->data==data) return p; else return NULL; } pnode insertfirst(pnode phead,int finedata, int data)//在finedata元素的前面插入data { pnode pnew=new node; pnew->data=data; pnode cur=phead; pnode pre=NULL; while(cur->next!=phead) { if(cur->data==finedata) { if(cur==phead) { pnode p=phead; while(p->next!=phead) { p=p->next; } p->next=pnew; pnew->next=phead; phead=pnew; } else { pre->next=pnew; pnew->next=cur; } return phead; } pre=cur; cur=cur->next; } if(cur->data==finedata) { pre->next=pnew; pnew->next=cur; } return phead; } pnode insertfirstback(pnode phead,int finedata, int data)//在finedata元素的后面插入data { pnode pnew=new node; pnew->data=data; pnode cur=phead; pnode pre=NULL; while(cur->next!=phead) { if(cur->data==finedata) { if(cur==phead) { pnew->next=phead->next; phead->next=pnew; } else { pnew->next=cur->next; cur->next=pnew; } return phead; } pre=cur; cur=cur->next; } if(cur->data==finedata) { cur->next=pnew; pnew->next=phead; } return phead; } void showall(pnode phead) { if(!phead) return; pnode p=phead; while(p->next!=phead) { printf("%6d,%6d,%2d\n",p,p->next,p->data); p=p->next; } printf("%6d,%6d,%2d\n",p,p->next,p->data); } int getnum(pnode phead) { if(phead==NULL) return 0; else if(phead->next==phead) return 1; else { int count=1; pnode save=phead; while(save->next!=phead) { count++; save=save->next; } return count; } }
#include "Josefu.h" int main() { #define N 10 pnode phead=NULL; for( int i=0; i<N; i++) phead=addback(phead,i); showall(phead); printf("\n\n\n"); pnode p=phead; while(getnum(phead)!=1) { for( int i=0; i<2; i++) { p=p->next; } phead=pdeletefirst(phead,p->data,&p); } showall(phead); return 0; }
相关文章推荐
- //有500个人坐成一圈从第一个开始报数为3时出列 再从1开始遇到3出列 依次下去 直到圈里只有一个人时 求它原来在内圈的位置
- 有17个人围成一圈(编号为0~16),从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止。问此人原来的位置是多少号?
- java 有n个人围成一圈,从第1个人开始,1、2、3报数,报至3出局,余下的人继续从1、2、3报数,问:最后剩下的一人是原来的第几号?同时求出被淘汰编号的序列
- n个人围成一圈,从第1个人开始报数,每报到第m个人,则其出局,求最后出局的人的初始序号
- 有10(100人或者n个)个人围成一圈,从第一个人开始报数(报1、2、3),只要报3的人就出去, 然后不停的循环报数,直到最后剩下一个人,计算出这个人最开始的位置。其实就是约瑟夫环问题
- 有n个人围成一圈,从第1个人开始,1、2、3报数,报至3出局,余下的人继续从1、2、3报数,问:最后剩下的一人是原来的第几号?同时求出被淘汰编号的序列。(要求:用循环队列解决该问题。)
- 强化练习4:有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数), 凡报到3的人退出圈子,问最后留下的是原来第几号。
- 面试题:有20个人围成一圈,顺序排号。从第一个人开始报数(从1到3报 数),凡报到3的人退出圈子
- 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
- 【嵌入式系统学习记录】练习题:有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位?
- 【转】 小算法:n个人围成一圈,从第一个人开始报数,报到3的倍数的退出,一直到剩下最后一个人
- java 报数游戏:有n个孩子站成一圈,从第一个孩子开始顺时针方向报数,报到m的人出列
- 有N个人围成一圈,第一个人从1开始报数,报到M的人出列,求最后一个出列的人。
- 有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位
- 有N个人围成一圈,顺序排列.从第一个人开始报数(从1—>3),凡报到3的退出圈子。问最后留下的是原来第几号的那位?
- 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那个人。
- Java 写有n个人围成一圈,顺序排号,从第一个人开始报数(从1~3报数),凡报到3的人退出圈子,问最后留下的人原来排在第几号。
- 有n个人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来的第几号的那位
- 有n个人围成一圈编号1~n,顺序排好,从第一个人开始1到3报数,凡是报到3的人退出圈子,C语言编程出圈顺序
- 题目:有 n 个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位。