约瑟夫问题

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述
约瑟夫问题:有n只猴子,按顺时针方向围成一圈选大王(编号从1到n),从第1号开始报数,一直数到m,数到m的猴子退出圈外,剩下的猴子再接着从1开始报数。就这样,直到圈内只剩下一只猴子时,这个猴子就是猴王,编程求输入n,m后,输出最后猴王的编号。

输入
每行是用空格分开的两个整数,第一个是 n, 第二个是 m ( 0 < m,n <=300)。最后一行是:

0 0

输出
对于每行输入数据(最后一行除外),输出数据也是一行,即最后猴王的编号
样例输入
6 2
12 4
8 3
0 0
样例输出
5
1
7
  既然这题的归类是链表与指针,那首先就是用链表模拟啦,注意特殊情况如 N=1,M=1 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<cstring>
 7 using namespace std;
 8 int next[3000];
 9 int last[3000];
10 bool vis[3000];
11 int N,M;
12 int now;
13 int num;
14 int cnt;
15 bool jud;
16 int main(){
17     for(;;){
18         memset(next,0,sizeof(next));
19         memset(last,0,sizeof(last));
20         memset(vis,false,sizeof(vis));
21         scanf("%d%d",&N,&M);
22         if(N==1){
23             cout<<1<<endl;
24             continue;
25         }
26         if(N==M&&M==0){
27             return 0;
28         }
29         for(int i=1;i<=N;i++){
30             if(i!=N) next[i]=i+1,last[i+1]=i;
31             else next[N]=1,last[1]=N;
32         }
33         now=num=1; jud=false;
34         cnt=N;
35         for(;;){
36             if(now==N+1) now=1;
37             if(num==M&&vis[now]==false){
38                 num=1;
39                 next[last[now]]=next[now];
40                 last[next[now]]=last[now];
41                 vis[now]=true;
42                 cnt--;
43                 if(cnt==1){
44                     for(int i=1;i<=N;i++){
45                         if(vis[i]==false){
46                             cout<<i<<endl;
47                             jud=true;
48                             break;
49                         }
50                     }
51                 }
52                 if(jud==true) break;
53                 now++;
54             }
55             else if(vis[now]==true) now++;
56             else if(vis[now]==false) now++,num++;
57         }
58     }
59     return 0;
60 } 

  还可以:

 1 #include<iostream>
 2 using namespace std;
 3 int a[301];
 4 int main(){
 5     int n,m;
 6     while(1){
 7          cin>>n>>m;
 8          if(m==0&&n==0) return 0;
 9          a[1]=0;
10          for(int i=2;i<=n;i++){
11              a[i]=(a[i-1]+m)%i;
12          }
13          cout<<a[n]+1<<endl;
14      }
15 }

 

 
posted @ 2015-10-23 23:44  CXCXCXC  阅读(220)  评论(0编辑  收藏  举报