WA for Interactive Problem

Why i’m getting wrong answer for the problem “Guess The Number(Interactive problem)” if i use ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

Link for the problem: Contest Page | CodeChef

My solution for the problem :

NOTE: This code is giving WA;

using namespace std;
#include<bits/stdc++.h>
using ll=long long int;
#define endl “\n”
#define pb push_back

int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

vector<ll>v;

for(ll i=1;i<1001;i++)
v.push_back(i*i);

ll t,ans;
cin>>t;

while(t--)
{
  
           for(ll i=0;i<1000;i++)
           {
             cout<<v[i]<<endl;
             cin>>ans;
             if(ans==1)break;
           }
 }

}

But the following gives AC

using namespace std;
#include<bits/stdc++.h>
using ll=long long int;
#define endl “\n”
#define pb push_back

int main()
{
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

vector<ll>v;

for(ll i=1;i<1001;i++)
v.push_back(i*i);

ll t,ans;
cin>>t;

while(t--)
{
  
           for(ll i=0;i<1000;i++)
           {
             cout<<v[i]<<endl;
             cin>>ans;
             if(ans==1)break;
           }
 }

} Only difference in both code is the use of fast I/O.
so ,why cant we use fast I/O for interactive problems?

My Solution works fine even with fastio.

1 Like

Can you try removing this?

2 Likes

Then,what’s wrong in my solution?

either try what ssjgz said or you can also change tocout << v[i] << endl << flush;.

If my suggestion worked, then it’s because you weren’t flushing the output when fastio was being used.

I’m fairly surprised it worked without fastio.

Edit:

Oh, of course it worked without fastio - tie'ing the streams means that a call to cin flushes the output. Silly me :slight_smile:

Yeah, it works fine.

But what’s the problem with that if I define endl as \n.
Can you please explain it?

endl means “new line, then flush output”.

\n just means “new line”.

Edit:

See e.g. c++ - "std::endl" vs "\n" - Stack Overflow.

Maybe this will make some sense… I don’t know much though.

Thanks for your response.
It also works fine.

Thanks,i get it now.

Then how did this got accepted, if \n doesn’t flush the output?

See my edit.

May be there was no buffer to flush but its good to use endl instead of “\n” in interactive problems.

1 Like