fflush() C++ codeforces

Правка en1, от HaiZuka, 2021-02-06 07:58:30

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.

Теги #c++, fflush()

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский HaiZuka 2021-02-06 07:58:30 1662 Bản sửa đổi ban đầu (published)