Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
108962 彭士宝 18排列骨头 C++ Time Limit Exceeded 1000 MS 260 KB 1735 2025-01-23 17:30:58

Tests(0/1):


Code:

#include <iostream> #include <vector> using namespace std; // 模拟过程,检查当前排列是否符合条件 bool simulate(const vector<int>& arr) { int n = arr.size(); vector<bool> removed(n, false); // 标记是否被取出 int count = 0; // 当前数到的数字 int removedCount = 0; // 已取出的数量 int pos = 0; // 当前位置 while (removedCount < 15) { // 取出15个后停止 if (!removed[pos]) { // 如果当前位置未被取出 count++; if (count == 9) { // 数到9 removed[pos] = true; removedCount++; count = 0; // 重置计数 } } pos = (pos + 1) % n; // 移动到下一个位置 } // 检查剩下的15个位置是否全是骨头 int boneCount = 0; for (int i = 0; i < n; i++) { if (!removed[i] && arr[i] == 1) { // 如果剩下的位置是骨头 boneCount++; } } return boneCount == 15; // 如果剩下的全是骨头,返回true } int main() { vector<int> arr(30, 0); // 初始化数组,0表示胡萝卜 for (int i = 0; i < 15; i++) { arr[i] = 1; // 前15个位置放骨头 } // 模拟找到符合条件的排列 while (!simulate(arr)) { // 如果当前排列不符合条件,调整排列 int temp = arr.back(); arr.pop_back(); arr.insert(arr.begin(), temp); } // 输出符合条件的排列 for (int i = 0; i < 30; i++) { if (arr[i] == 1) { cout << "骨头 "; } else { cout << "胡萝卜 "; } } cout << endl; return 0; }