https://www.cnblogs.com/lanxuezaipiao/p/3339603.html
有N个人围一圈依次报数,数到3的倍数的人出列,问当只剩一个人时他原来的位子在哪里?
解答:经典的转圈踢人问题,好吧专业一点,约瑟夫环问题,相信大家都会,下面给我的code:
int main() {int N, i, j;printf("Please enter the number of people(N): ");scanf("%d", &N);int *pArray = (int *)malloc(sizeof(int) * N);int count = 0;// 这里编号为0 ~ N - 1for(i = 0; i < N; i++){pArray[i] = i;}for(i = 0, j = 0; i < N; i = (i + 1) % N){if(pArray[i] != -1){j++;if(j % 3 == 0){pArray[i] = -1;count++;if(count == N){printf("The last people is %d\n", i);break;}}}}return 0; }
好吧,我承认我的算法很臃肿,完全是模拟了整个游戏过程,时间复杂度为O(mn),这里m=3,网上有个大牛给出了归纳数学的方法,具体方法如下: