Doubt in the code

, ,




For the above attached question, I am trying to solve the question . When I’m running the code in vs code I am getting the proper output but here when sumbitted it shows wrong answer. Anyone please help me. I am attaching my code below;

#include <stdio.h>

int main(void)

{

int T;

scanf("%d",&T);

while(T--)

{

    int a,b,c,d; // initial coins all four have

    printf("Enter the number of coins Nitin has initially: ");

    scanf("%d",&a);

    printf("Enter the number of coins Sobhagya has initially: ");

    scanf("%d",&b);

    printf("Enter the number of coins Ritik has: ");

    scanf("%d",&c);

    printf("Enter the number of coins Satyarth has: ");

    scanf("%d",&d);

    if (a>=b)

    {

       int e=b+c; // ritik gives to sobhagya

       if (a>=e) // still nitin has more

       {

           int f=e+d; // satyarth gives to sobhagya

           if(a>=f)

           {

               printf("N \n");

           }

           else

           {

               printf("S \n");

           }          

       }

       else  // sabhogya has more

       {

           int g=d+a; // ritik gives to nitin

           if (g>=e)

           {

               printf("N \n");

           }

           else

           {

               printf("S \n");

           }

       }

    }

    else   //if sabhogya has more

    {

        int h=a+c;  // ritik gives to nitin

        if (h>=b)  // nitin has more

        {

            int i=d+b; // satyarth gives to sobhagya

            if(h>=i)  // nitin has more

            {

                printf("N \n");

            }

            else

            {

                printf("S \n");

            }

        }

        else // sobhagya has more          

        {

            int j=h+d; // satyarth gives to nitin

            if(j>=b) // nitin has more

            {

                printf("N \n");

            }

            else{

                printf("S \n");

            }

        }

    }

}

return 0;  

}

you don’t need to print-enter the number of coins ‘x’ has, you have to strictly follow the input format
remove the printf statement and just use scanf for taking input

and try use as much less variables as possible

1 Like

thank you so much for helping. i was able to solve it successfully after doing the changes recommended. Thanks again!

#include <bits/stdc++.h>
using namespace std;

int main() {
int T;
cin>>T;
while (T–) {
int a,b,c,d;
cin>>a>>b>>c>>d;

    vector<int>v;
    v.push_back(a);
    v.push_back(b);
    v.push_back(c);
    v.push_back(d);

    int N=v[0],S=v[1];

    for(int i=2;i<v.size();i++){
        if(N==S){
            S += v[i];
        }
        else if(N>S){
            S += v[i];
        }
        else{
            N += v[i];
        }
    }

    if(N==S){
        cout<<"N"<<endl;
    }
    else if(N>S){
        cout<<"N"<<endl;
    }
    else{
        cout<<"S"<<endl;
    }

}

return 0;

}

This is my solution, i just pushed it into a vector and did the thing you can easily understand with the code