`
huobengle
  • 浏览: 860731 次
文章分类
社区版块
存档分类
最新评论

数据结构-猴子选王

 
阅读更多

一堆猴子都有编号,编号是1,2,3 ...m ,这群猴子(m个)按照1-m的顺序围坐一圈,从第1开始数,每数到第N个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子,则该猴子为大王。
要求:输入数据:输入m,n ,m,n 为整数,n<m
输出形式:提示按照m个猴子,数n 个数的方法,输出为大王的猴子是几号 ,建立一个函数来实现此功能。

//猴子选王
//程序:张建波
//时间:05/07/05
#include <iostream.h>

typedef struct Node{
int data;
struct Node *next;
}NODE;

int _f6_main() //主函数,也是f6的入口
{

NODE *head,*s,*q,*t;
int n,m,count=0,i;
cout<<"猴子选王程序/n";
cout<<"请输入猴子个数n,m号"<<endl;
cout<<"n=";
cin>>n;
cout<<"/nm=";
cin>>m;


for(i=1;i<=n;i++){
s=new NODE;
s->data=i;
s->next=NULL;
if(i==1){head=s,q=head;}
else
{
q->next=s;
q=q->next;
}
}
q->next=head;
cout<<"/n出队前:";
q=head;
while(q->next!=head)
{
cout<<q->data<<" ";
q=q->next;
}
cout<<q->data;
cout<<"/n出队后:";
q=head;
do
{
count++;
if(count==m-1)
{
t=q->next;
q->next=t->next;
count=0;
cout<<t->data<<" ";
}
q=q->next;
}while(q->next!=q);
cout<<q->data<<endl;

return 0;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics