1012 Joseph simulation

This classic entitled Joseph problems.

There Joseph classical formula: ans [i] = (ans [i-1] + m)% i i represents the number of individual packets, the number of the final winner.

This question is different from the original 2k Joseph problem is that there are individuals, after going to kill before k k retain people. Joseph and the original problem is to kill everyone besides his own. Therefore, attention should write code.

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3  the using  namespace STD;
 . 4  int main () {
 . 5      int K = 0 , = n- 0 , ANS [ 15 ], COUNT [ 20 is ], m; // ANS [I ] represents the minimum number m and kill all the bad guys while leaving k individuals.
6                                  // COUNT [i] represents the number of individual i when every newspaper report the number of people killed m. 
. 7      COUNT [ 0 ] = 0 ;
 . 8      Memset (ANS, 0 , the sizeof (ANS));
 . 9      the while (CIN >> K) {
 10          IF (K ==0 )
 . 11              BREAK ;
 12 is          m = . 1 ;
 13 is          n-= 2 * K;
 14          for ( int I = . 1 ; I <= K; I ++ ) {
 15              COUNT [I] = (COUNT [I- . 1 ] + (M- . 1 ))% (I-n-+ . 1 ); // time later for the sake of divisor numbers, taking into account the number of m may be greater than survivors, once all of the new round of a case where only one kill. 
16              IF (COUNT [I] <K) { // immediate concern the good, then this does not meet the requirements of m, m is incremented, I start over. 
. 17                  m ++ ;
 18 is                  I = 0 ;
 . 19             }
20         }
21         ans[k]=m;
22         cout<<ans[k]<<endl;
23     }
24     return 0;
25 }

 

Guess you like

Origin www.cnblogs.com/sweet-ginger-candy/p/11516842.html