代码摘录只有在使用完整代码时才会意外冻结

Code excerpt freezes unexpectedly only when with full code

是什么导致我的代码出现意外行为?

程序在到达预期的代码部分之前冻结(不是崩溃)。该程序完全包含在 main() 中,将整个代码与预期的语句隔离开来使其正常工作。为什么会这样?


我正在为 this codeforces problem, that I was intending to refine little by little. The problem is that curiously my program freezes when reading the input (like if it was an infinite loop, it doesn't crash). I tried both C++ and C++11 on GCC, and both of them froze. Tried Ideone 编写一个糟糕的解决方案,同样的事情发生了。它可以是任何东西,只是我将 一切 从第一个包含复制到输出行,这将确认所有输入都被读取并且 运行 只有这个摘录。

#include <bits/stdc++.h>
using namespace std;

typedef unsigned uint;

int main() {
    ios_base::sync_with_stdio(false);
    uint n, h, k, buf;
    vector<uint> potatoes;

    cin >> n >> h >> k;

    for (uint i = 0; i < n; ++i)
    {
        cin >> buf;
        potatoes.push_back(buf);
    }

    cout << "Letf\n";

    return 0;
}

这是一个简化版本,包含输入级逻辑的所有行。预期输入是

5 6 3

5 4 3 2 1

这是 full code and the correctly working excerpt 的链接。

主要问题是你的 while (true) {...}。你的 "algorithm" 使这个循环无限。

如果你不知道,已经有该回合的教程和源代码 http://codeforces.com/blog/entry/45181

最后请仔细看问题页面。竞赛材料部分有对您有用的东西。