Actually I came to know about fast input/output through following answer, https://stackoverflow.com/a/31165481/6108030.
Question 1:
It says that if I use ios_base::sync_with_stdio(false);
, it will disable the synchronization between C and C++ style input/output.
So according to my interpretation it means that by using this statement I won't be able to use scanf()
(i.e C style I/O) in my C++ code (correct me if I got it wrong).
But my code is still working even after using ios_base::sync_with_stdio(false);
.
Here's the code.
int main()
{
ios_base::sync_with_stdio(false);
long test; cin>>test;
while(test--)
{
ull f=0,t=0,a,b,c,d,k;
scanf("%llu %llu %llu %llu %llu",&a,&b,&c,&d,&k); //scanf() used
while(f<k){ //
t++; //Irrelevant from a question perspective
f=a*(t*t*t)+b*(t*t)+c*t+d; //
} //
if(f==k)
cout<<(t)<<"\n";
else
cout<<(t-1)<<"\n";
}}
/* Expected behaviour
2 input(no of test)
2 2 2 2 10 input(a b c d k)
1 output
2 3 5 7 1000 input
7 output
Behavior of this code
2
2 2 2 2 10 input
2 3 5 7 1000 input(prompted)
1 output
7 output
*/
What could be the right reason behind this?
Question 2:
The answer also stated that using cin.tie(NULL)
unties cin
from cout
.
But in the above code when I just used cin.tie(NULL)
and not ios_base::sync_with_stdio(false);
the output was same as mentioned above in the "Expected behavior" section instead of that in "Behavior of this code" section.
Also I want to know whether ios_base::sync_with_stdio(false);
and cin.tie(NULL)
are interrelated and should be used together or not?
I am looking for a comprehensive explanation for this behavior. It will be more helpful if you'll take my input samples for the explanation.