Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
70179 LJY 18排列骨头 C++ Wrong Answer 1 MS 264 KB 1028 2024-04-13 17:57:02

Tests(0/1):


Code:

#include <iostream> #include <vector> using namespace std; // 函数用于求解约瑟夫斯问题 vector<int> josephus(int total, int step) { vector<int> result; vector<int> items; // 初始化序列 for (int i = 1; i <= total; ++i) { items.push_back(i); } int index = 0; while (items.size() > total / 2) { // 计算要删除的位置 index = (index + step - 1) % items.size(); // 将删除的位置加入结果序列 result.push_back(items[index]); // 从序列中删除该位置的元素 items.erase(items.begin() + index); } return result; } int main() { int total = 15; // 总共的物品数 int step = 9; // 每次数数的步长 vector<int> result = josephus(total, step); cout << "最后剩下的15根骨头的排列顺序为:" << endl; for (int i = 0; i < result.size(); ++i) { cout << result[i] << " "; } cout << endl; return 0; }


Run Info:

------Input------
0
------Answer-----
1 2 3 4 10 11 13 14 15 17 20 21 25 28 29
------Your output-----
最后剩下的15根骨头的排列顺序为: 9 3 13 8 5 2 1 4