joseph (m的值可变)

这题 是 数据结构 书上的习题,写一个 循环链表;

话说 上课的时候 竟然 花了 3节课写了一个 不完整 的解题代码;

回来以后 重写 完善了;

不过 这个 代码有些 纰漏,就是 无法用 freopen 这个函数,用了有问题...没调试出来;

因为 链表不怎么用,所以写链表的时候 花了快一节课;

回来写了以后五分钟就搞定了..

代码多写写自然就得心顺手了...

 1 #include<iostream>
 2 #define maxn 1000
 3 using namespace std;
 4 int m,n;
 5 struct node
 6 {
 7     int num;
 8     int code;
 9     node * next ;
10 };
11 void create(node * phead,int len)
12 {
13     node * p =new node;
14       p = phead;
15     cin>> p-> code ;
16     p-> num = 1;
17    int i;
18    for(i= 1 ; i< len ;i++)
19    {
20        node * s =new node ;
21        cin>> s-> code ;
22        s-> num = i+1;
23         p-> next = s ;
24         p = s;
25    }
26    p->next = phead ;
27 }
28 int main()
29 {
30     //freopen("data.txt","r",stdin);
31      int i,count,t=1;
32      bool flag;
33      printf("请分别 输入初始密码 m 和 人数 n
");
34     while(scanf("%d%d",&m,&n),m,n)
35     { 
36      node * p ,*s ,* head;
37      head =new node;
38      s =new node;
39      create( head ,n );
40      count = 0;
41      flag=true;
42      p= new node;     
43      p= head ; 
44      while(p->next != head )
45      {
46          p= p->next ;
47      } 
48      printf("Case %d :
",t++);
49      while( p->next != p )
50      {
51          count ++;
52          //printf("%d ",p->next->num);
53          if( count == m)
54          {
55              flag=false;
56              node * x = p->next ;
57              s = x->next ;
58              p-> next = s;
59              m = x->code ;
60              count = 0;
61              printf(" %d 号: 我先走啦,我后面的第 %d 个人我们一起走吧
",x->num,x->code);
62              delete x;
63          }
64          if(flag)
65            p=p->next;
66          flag=true;
67      }
68      printf(" %d 号: 怎么数第 %d 个人还是我自己
",p->num,p->code);
69      delete p;
70     }
71     return 0;
72 }
原文地址:https://www.cnblogs.com/xiaoniuniu/p/4037071.html