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

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

I tried to solve the following interactive problem: Problem link

Please focus in the main() function:

bool testcase =          0               ;
void solve()
{   
int hi = 1000000, lo = 1, mid;
    string s; 
    while(lo+1<hi)
    {
        mid = (lo+hi)/2;
        cout << mid << "\n";
        fflush(stdout);
        cin >> s;
        if(s[0]=='<') { hi = mid-1; }
        else { lo = mid; }
    }
    cout << hi << "\n";
    fflush(stdout);
    cin >> s;
    if(s[0]=='>') cout << "! " << hi << "\n";
    else cout << "! " << lo << "\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
   int t = 1; 
   if(testcase == true)
   {
  cin >> t;
   }
    while(t--)
   {
       solve();
   }
} 

When I ran the above piece of code in Visual Studio code terminal it didn't produce any output. Then I deleted those 2 lines in the main function.

  ios_base::sync_with_stdio(0);
    cin.tie(0);

Then it ran nicely producing desired output. Why it didn't work with those 2 lines of code?

Any insight will help a lot. Thanks for your patience.

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

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

Try modify fflush(stdout); to cout.flush();?

And remember to flush before the end of solve().