Блог пользователя HaiZuka

Автор HaiZuka, история, 4 года назад, По-английски

Hello everyone.

I recently entered the contest Codeforces Round 699 (Div. 2), specifically I did the exercise 1481A - Space Navigation , which asked me to read two integers qx, qy and finally a string character S. I use C ++ programming language to do this. I used the fflush(stdin) function to clear the count memory while doing this, specifically my main code at the time was:

vector <string> ans;

int main() {
    int t, a, b;
    string s;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> a >> b;
        fflush(stdin);
        getline(cin, s);
        ans.push_back(solve(a, b, s));
    }
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
}

The problem I have is that the codeforces system does not seem to be executing my fflush () function, while I am still able to run the correct program using my computer.

I have to fix my program to:

vector <string> ans;

int main() {
    int t, a, b;
    string s;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> a >> b;
        cin >> s;
        ans.push_back(solve(a, b, s));
    }
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
}

With this program, the system accepted my program.

However, entering a string using the function std :: cin is not a good choice, if there is ' '(whitespace, # 32 ASCII) in the string then I don't really know how to read the data. :(

I write this article to ask you to share your experiences about the problem that I am facing to help my upcoming competitions go smoother.

Thank you for reading my article, good day.

  • Проголосовать: нравится
  • -5
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

I'm not sure what fflush is supposed to do here, but you can use cin.ignore to ignore the stream until a '\n' is encountered.

See this article for a detailed answer.