Please read the new rule regarding the restriction on the use of AI tools. ×

AIC's blog

By AIC, history, 4 years ago, In English

Have a look at the code below all three print statements at the end of main function are giving different results, can someone please explain why is it so??

Input :: 4 4 3 4 3 2 1 1 0.3 3 1 4 0.6 5 3 4 2 1 3 5 3 0.8 4 0.6 5 0.3 6 5 1 3 2 4 5 6 4 0.9 5 0.3 2 0.4 6 0.7 3 0.5 4 2 1 2 3 4 2 0.5 4 0.1

void MAIN()
{
    int n,m;
    cin>>n>>m;
    int x=0;
    for(int i=1;i<=n;i++)
    {
        int a;
        cin>>a;
        if(a!=i)
        {
            x=i;
        }
    }

    float arr[n+1]={0};
    for(int i=1;i<=m;i++)
    {
        int a;
        cin>>a;
        float b;
        cin>>b;
        arr[a]=b;
    }

    if(x==0)
    {
        cout<<1<<endl;
        return;
    }

    float p=1.0;
    float ans=0;
    for(int i=n;i>=x;i-- )
    {
        ans+=p*arr[i];
        p*=(1.0-arr[i]);

    }

    //printf("%f\n", ans);
    cout<<ans<<"\n";
    //cout<<ans<<endl; 
}
 
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin>>t;
    while(t--)
    {
         MAIN();
    }
   
}

  • Vote: I like it
  • -1
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +11 Vote: I do not like it

I can't understand your code with your bad formatting. Please format your document. (At least, look at your blog entry once more using "Preview" and make it sensible.)

Do not mix printf and cout (C-style and C++-style) printer after using ios_base::sync_with_stdio(false). You can infer what the statement means, or if you cannot, I have good reference document for you.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Don't use printf and scanf while using fast Input Output. You can search google for reason.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    nope, there are many fast input-output tricks that not use ios_base::sync_with_stdio(0) which is in his case causing the behavior