solution.md 2.5 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 约瑟夫问题


<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>

ToTensor's avatar
ToTensor 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
以下程序实现了这一功能,请你填补空白处的内容:

```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;
ToTensor's avatar
ToTensor 已提交
34
        ______________;
ToTensor's avatar
ToTensor 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        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;
}
```

每日一练社区's avatar
每日一练社区 已提交
53 54 55 56
## template

```cpp
#include <iostream>
ToTensor's avatar
ToTensor 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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";
每日一练社区's avatar
每日一练社区 已提交
76
}
ToTensor's avatar
ToTensor 已提交
77 78 79 80 81 82 83 84 85 86
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;
每日一练社区's avatar
每日一练社区 已提交
87 88 89 90 91 92
}
```

## 答案

```cpp
ToTensor's avatar
ToTensor 已提交
93 94 95
for (i = index; i < sum - 1; i++)
    arr[i] = arr[i + 1];
start = index + 1;
每日一练社区's avatar
每日一练社区 已提交
96 97 98 99 100 101 102
```

## 选项

### A

```cpp
ToTensor's avatar
ToTensor 已提交
103 104 105
for (i = index; i < sum - 1; i++)
    arr[i] = arr[i + 1];
start = index - 1;
每日一练社区's avatar
每日一练社区 已提交
106 107 108 109 110
```

### B

```cpp
ToTensor's avatar
ToTensor 已提交
111 112 113
for (i = index; i < sum; i++)
    arr[i] = arr[i - 1];
start = index + 1;
每日一练社区's avatar
每日一练社区 已提交
114 115 116 117 118
```

### C

```cpp
ToTensor's avatar
ToTensor 已提交
119 120 121
for (i = index; i < sum; i++)
    arr[i] = arr[i - 1];
start = index - 1;
每日一练社区's avatar
每日一练社区 已提交
122
```