# 约瑟夫问题


<p>有n人&#xff08;编号分别为1到n号&#xff09;围成一圈&#xff0c;从第s人开始报数&#xff0c;报到第m的人出列&#xff0c;然后从出列的下一人重新开始报数&#xff0c;报到第m的人又出列&#xff0c;……&#xff0c;如此重复直到n-1全部出列&#xff0c;只剩最后一个人为止。求剩下的最后一人是谁&#xff1f;
输入
一行&#xff0c;三个整数n&#xff0c;m&#xff0c;s&#xff0c;&#xff08;0&lt;&#61;n&#xff0c;m&#xff0c;s&lt;&#61;1000&#xff09;意义如上。
输出
一行&#xff0c;一个正整数&#xff0c;表示最后剩下的人的编号。
输入样例</p>
<pre>
<code class="language-html">8 3 1</code></pre>
<p>输出样例</p>
<pre>
<code class="language-html">7</code>
</pre>
<p>要求从第s个数开始&#xff0c;数m个出列&#xff0c;第s个不出列</p>

以下程序实现了这一功能，请你填补空白处的内容：

```cpp
#include <iostream>
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 <iostream>
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;
```