简单模拟题,数数的时候稍微细心点就行了。
#include <stdio.h> #include <string.h> int people[26]; int N; int joseph[14]; void init(){ int i; for(i=0;i<N;i++){ people[i]=1; } for(i=N;i<2*N;i++){ people[i]=0; } } int go(int steps){ int n=2*N; int i=0; int j; while(n>N){ i=(i+steps-1)%n; n--; if(people[i]){ return 0; } for(j=i+1;j<N;j++){ people[j-1]=people[j]; } } return 1; } int main(void){ for(N=1;N<14;N++){ int i=N; int S=2*N; while(1){ if((i-1)%S<N){ i++; continue; } init(); if(go(i)){ joseph[N]=i; break; } i++; } } while(1){ scanf("%d",&N); if(N==0){ break; } printf("%d\n",joseph[N]); } return 0; }