Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
121786 胡海峰老师 18排列骨头 C++ Accepted 0 MS 268 KB 1592 2025-06-08 11:24:03

Tests(1/1):


Code:

#include <iostream> #include <vector> using namespace std; int main() { const int total = 30; // 总共有30个位置(15胡萝卜+15骨头) const int k = 9; // 每次数到9时移除一个 const int remain = 15; // 需要剩下15个骨头 vector<bool> is_bone(total, false); // false表示胡萝卜,true表示骨头 vector<int> bone_positions; // 存储骨头应该放置的位置 // 我们需要找到15个位置放置骨头,使得最后剩下的15个全是骨头 // 这是一个反向约瑟夫问题,我们需要找出哪些位置不会被移除 // 模拟约瑟夫过程,标记最后剩下的15个位置 vector<bool> removed(total, false); int current = 0; int remaining = total; while (remaining > remain) { // 数k-1个未被移除的元素 int count = 0; while (count < k) { if (!removed[current]) { count++; if (count == k) break; } current = (current + 1) % total; } // 标记当前元素为已移除 removed[current] = true; remaining--; } // 收集未被移除的位置(这些位置应该放置骨头) for (int i = 0; i < total; ++i) { if (!removed[i]) { bone_positions.push_back(i + 1); // 位置从1开始编号 } } // 输出骨头应该放置的位置 for (int pos : bone_positions) { cout << pos << " "; } cout << endl; return 0; }