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

Tests(1/1):


Code:

#include <iostream> using namespace std; const int TOTAL = 30; // 总共有30个位置(15胡萝卜+15骨头) const int K = 9; // 每次数到9时移除一个 const int REMAIN = 15; // 需要剩下15个骨头 int main() { bool removed[TOTAL] = {false}; // 标记是否被移除 int bone_positions[REMAIN]; // 存储骨头应该放置的位置 int bone_count = 0; // 已找到的骨头位置数 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[bone_count++] = i + 1; // 位置从1开始编号 if (bone_count == REMAIN) break; } } // 输出骨头应该放置的位置 for (int i = 0; i < REMAIN; ++i) { cout << bone_positions[i] << " "; } cout << endl; return 0; }