xiangel's blog

By xiangel, 9 years ago, In English

The problem is http://codeforces.com/gym/100519/problem/I.

The problem request you to guess a prime P, P is less than or equals to N, and N is the first line in a test case.

Your program should make some guesses and the judge will return a answer string when your program do that. It confused me on the I/O.

Here are my two different codes:

while (cin >> n) {
    do {
        cout << my guess number << endl;
        cin >> ans;                  // ans is answer string
        do something.
    } while (it should keep going);
    if (ans[0] == 'O')               // means the answer string is "OK"
        continue;
    get the P.
    cout << P << endl;
    cin >> ans;                      // it will always return "OK" when you guess is current
}
while (scanf("%d", &n) != EOF) {
    do {
        printf("%d\n", my guess number);
        scanf("%s", ans);            // ans is answer string
        do something.
    } while (it should keep going); 
    if (ans[0] == 'O')               // means the answer string is "OK"
        continue;
    get the P.
    printf("%d\n", P);
    scanf("%s", ans);                // it will always return "OK" when you guess is current
}

The first one get Ac, and the second one get "Idleness limit exceeded".

Could anyone help me?

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 years ago, # |
Rev. 2   Vote: I like it +10 Vote: I do not like it

Well I'm not sure about that but I think most interactive problems require flushing the output after each thing you print, so the judge's program can get access to it. This is done by fflush(stdout); for example.

My guess is that the first program works, since cout<<endl; automatically flushes the output, while printf doesn't.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Oh! It's worked when I add "fflush(stdout)" after each printf. Thank you very much!