COJ 1056 【数据结构】约瑟夫(Josephus)问题

COJ 1056 【数据结构】约瑟夫(Josephus)问题
 1  #include  < cstdlib >
 2  #include  < iostream >
 3  #include  < cstring >
 4 
 5  using   namespace  std;
 6 
 7  int  a[ 10000 ];
 8  int  main( int  argc,  char   * argv[])
 9  {
10       int  m,n,i,j,k,r;
11       // cin>>m>>n;
12       while  (scanf( " %d%d " , & m, & n) &&! (m == 0 && n == 0 ))
13      {
14      
15           for (i = 0 ;i <= m;i ++ )
16              a[i] = i;
17          r = 0 ;k = 0 ;i = 1 ;    
18           while (r < m - 1 )
19          {
20              
21               if (a[i] != 0 )k ++ ;        
22               if (k == n)
23              {
24      
25                   // cout<<a[i]<<endl;
26                  a[i] = 0 ;
27                  k = 0 ;
28                  r ++ ;
29       //             for(j=1;j<=m;j++)
30       //                 cout<<a[j]<<endl;
31              }
32              i ++ ;
33               if (i == (m + 1 ))i = 1 ;
34          }
35          i = 0 ;
36           while (a[i] == 0 )
37              i ++ ;
38          cout << a[i] << endl;
39      
40      }
41      system( " PAUSE " );
42       return  EXIT_SUCCESS;
43  }
44 

你可能感兴趣的:(COJ 1056 【数据结构】约瑟夫(Josephus)问题)