# 约瑟夫问题

有n人(编号分别为1到n号)围成一圈,从第s人开始报数,报到第m的人出列,然后从出列的下一人重新开始报数,报到第m的人又出列,……,如此重复直到n-1全部出列,只剩最后一个人为止。求剩下的最后一人是谁? 输入 一行,三个整数n,m,s,(0<=n,m,s<=1000)意义如上。 输出 一行,一个正整数,表示最后剩下的人的编号。 输入样例

8 3 1

输出样例

7

要求从第s个数开始,数m个出列,第s个不出列

以下程序实现了这一功能,请你填补空白处的内容: ```cpp #include void JosePhus(int n, int m, int start) { int i, *arr = new int[n]; int count = 1; ; for (i = 0; i < n; i++) arr[i] = i + 1; int sum = n; while (count < n) { start--; int index = (start + m - 1) % sum; ______________; sum--; count++; } std::cout << arr[0] << "\n"; } int main(int argc, const char *argv[]) { int n, m, start; std::cout << "请输入n,m,start:\n"; while (std::cin >> n >> m >> start) { JosePhus(n, m, start); std::cout << "请输入n,m,start:\n"; } return 0; } ``` ## template ```cpp #include void JosePhus(int n, int m, int start) { int i, *arr = new int[n]; int count = 1; ; for (i = 0; i < n; i++) arr[i] = i + 1; int sum = n; while (count < n) { start--; int index = (start + m - 1) % sum; for (i = index; i < sum - 1; i++) arr[i] = arr[i + 1]; start = index + 1; sum--; count++; } std::cout << arr[0] << "\n"; } int main(int argc, const char *argv[]) { int n, m, start; std::cout << "请输入n,m,start:\n"; while (std::cin >> n >> m >> start) { JosePhus(n, m, start); std::cout << "请输入n,m,start:\n"; } return 0; } ``` ## 答案 ```cpp for (i = index; i < sum - 1; i++) arr[i] = arr[i + 1]; start = index + 1; ``` ## 选项 ### A ```cpp for (i = index; i < sum - 1; i++) arr[i] = arr[i + 1]; start = index - 1; ``` ### B ```cpp for (i = index; i < sum; i++) arr[i] = arr[i - 1]; start = index + 1; ``` ### C ```cpp for (i = index; i < sum; i++) arr[i] = arr[i - 1]; start = index - 1; ```