问题,n个人数到k被杀,最后活着的是谁?
代码
1 #include <cstdio> 2 #include <cstring> 3 4 //f(n,k) = (f(n-1,k)+k) %n 5 int yue[102]; 6 7 int main(int argc, char const *argv[]) 8 { 9 int n,k; 10 while(scanf("%d %d",&n, &k) != EOF) { 11 memset(yue, 0, sizeof(yue)); 12 yue[1] = 0; 13 for(int i = 2; i <= n; i++) { 14 yue[i] = (yue[i-1]+k)%i; 15 } 16 printf("%d ",yue[n]+1); 17 } 18 19 return 0; 20 } 21 22 /* 23 0 1 2 3 4 5 24 3 4 5 0 1 25 0 1 2 3 4 26 27 (0 + 3) % 6 = 3 28 (1 + 3) % 6 = 4 29 (4 + 3) % 6 = 1 30 */
如上22到25行
n个的情况的编号可由n-1个推出