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  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值