比较基础的约瑟夫环
记住约瑟夫环的递推的公式 就行了
假设是从0-n-1, 从0开始的第k个人
f[1]=0;
f[i]=(f[i-1]+k+1)%i (i>1)
Eeny Meeny Moo
Description Surely you have made the experience that when too many people use the Internet simultaneously, the net becomes very, very slow. Your job is to write a program that will read in a number of cities n and then determine the smallest integer m that will ensure that Ulm can surf the net while the rest of the country is cut off. Input The input will contain one or more lines, each line containing one integer n with 3 <= n < 150, representing the number of cities in the country. Output For each line of the input, print one line containing the integer m fulfilling the requirement specified above. Sample Input 3 4 5 6 7 8 9 10 11 12 0 Sample Output 2 5 2 4 3 11 2 3 8 16 Source |
#include <stdio.h> #include <string.h> int main() { int n; while(scanf("%d",&n)&&n) { int i; for(i=0;;i++) // 每隔i个报数 { int s=0; for(int j=2;j<=n-1;j++) { s=(s+i+1)%j; } if(s==0) break; } printf("%d\n",i+1); } return 0; }