如图所示的报数问题,请各位看看如何补全这串程序,谢谢了。就在图片所示的代码后面补写

关注【以下回答由 GPT 生成】
#include <iostream>
#include <list>
using namespace std;
int josephus(int n, int k) {
list<int> people;
for (int i = 1; i <= n; i++) {
people.push_back(i);
}
list<int>::iterator current = people.begin();
while (people.size() > 1) {
for (int count = 1; count < k; count++) {
current++;
if (current == people.end()) {
current = people.begin();
}
}
current = people.erase(current);
if (current == people.end()) {
current = people.begin();
}
}
return people.front();
}
int main() {
int n;
cout << "请输入总的人数:";
cin >> n;
int result = josephus(n, 3);
cout << "最后剩下的人的编号是:" << result << endl;
return 0;
}
以上是一个简单的Josephus问题的解决方案,使用了STL中的list容器来模拟围坐成圆圈的人员,通过迭代器来实现报数并删除淘汰的人。
【相关推荐】