2012华为2011年9月17日上机题目之约瑟夫环模拟法实现程序

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 #define MAXN 10
 6 
 7 struct Node
 8 {
 9     int index;
10     Node* next;
11 };
12 
13 class YSF
14 {
15     public:
16         int n;//规模
17         int m;
18         Node* top;
19         Node* bottom;
20         Node* t;
21         YSF(int ln,int lm)
22         {
23             n=ln;
24             m=lm;
25             top=new Node();
26             bottom=top;
27             top->index=0;
28             for(int i=1;i<n;i++)
29             {
30                 t=new Node();
31                 bottom->next=t;
32                 t->index=i;
33                 bottom=t;
34             }
35             bottom->next=top;
36         }
37         int getYSF()
38         {
39             int re=0;
40             Node* t;
41             for(int i=0;i<n-1;i++)
42             {
43                 for(int j=0;j<m-1;j++)
44                 {
45                     top=top->next;
46                 }
47                 //删除后面的一个结点 
48                 t=top->next->next;
49                 delete top->next;
50                 top->next=t;
51                 top=t;
52             }
53             return top->index;
54         }
55 };
56 
57 int main()
58 {
59     YSF* ysf=new YSF(10,6);
60     cout<<ysf->getYSF();
61     delete ysf->top;
62     getchar();
63 }
posted @ 2011-09-17 23:36  Eric.wei  阅读(915)  评论(0编辑  收藏  举报