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

Printf and cout giving different weird answers

Revision en4, by AIC, 2020-12-12 17:33:32

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();
    }
   
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English AIC 2020-12-12 17:33:32 31 Tiny change: ' }\n\n //print' -> ' }\n\n //print'
en3 English AIC 2020-12-12 17:29:19 0 (published)
en2 English AIC 2020-12-12 17:27:05 30 Tiny change: ' int a;\ncin>>a;\n ' -> ' int a;\n cin>>a;\n ' (saved to drafts)
en1 English AIC 2020-12-12 08:47:39 1330 Initial revision (published)