Why do I get Wrong Answer?

,

#include
#include
using namespace std;

int main() {

long long int t,i,n,w,c;

// your code goes here

cin>>t;



while(t--)

{c=0;
      cin>>n;

   char a[n],b[n];

  long long  int w[n+1];

    
    for(i=0;i<n;i++)

   { cin>>a[i];
   if(a[i]<65||a[i]>90)
   a[i]='_';
   }
  
    for(i=0;i<n;i++)

    { cin>>b[i];
   if(a[i]<65||a[i]>90)
   a[i]='_';
   }

    for(i=0;i<(n+1);i++)

    cin>>w[i];
   

    for(i=0;i<n;i++)

    {

    if(a[i]==b[i])

    c++;

    }
    cout<<w[c]<<endl;

}
return 0;
}

Why am I getting wrong answer?

Why am I getting a wrong answer?

#include
#include
using namespace std;
int main()
{
int a ;
double b;
cin >> a>> b;
if ((0< a<=2000) &&(0<=b<=2000) && (a <= b ))
{
if( a % 5 == 0 )

	cout <<fixed << setprecision(2)<<  b - a - 0.50 << "\n" ;

else 
     cout << fixed << setprecision(2) << b << "\n";

}
else
cout << fixed << setprecision(2) << b<< “\n”;
}

why m i getting wrong answer in this …question code CLEANUP?

#include<stdio.h>
main()
{
int T,t,n,m,i,k,N[1000],j,flag,chef[1000],asst[1000],x;
scanf("%d",&t);
for (T=0;T<t;T++)
{
scanf("%d",&n);
scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
N[i]=1;
}

   for(i=0;i<=m-1;i++)
   {
         scanf("%d",&x);
         N[x-1]=0;
         
       }
   flag=0;
   k=0;
   j=0;
   for(i=0;i<=n-1;i++)
   {
        if(N[i]==1)
        {
           if(flag==0)
           {
           chef[k]=i+1;
           k++;
           flag=1;        
           }
           else
           {
            asst[j]=i+1;
           j++;
           flag=0;  
          }
        }
   }
    for(i=0;i<=k-1;i++)
     {
        printf("%d",chef[i]);
     }
     printf("\n");
    for(i=0;i<=j-1;i++)
     {
        printf("%d",asst[i]);
     }
     printf("\n");
  }
   return 0;

}

#include<stdio.h>
#include<stdlib.h>
int main()
{
long long t,n,i,j,m,s,x,o,a,b;
scanf("%lld",&t);
while(t–)
{
o=0;
scanf("%lld",&n);
char A[n];
long long B[n];
scanf("%s",A);
for(i=0;i<n;i++)
scanf("%lld",&B[i]);
for(i=0;i<n;i++)
{
m=100000000;
if(A[i]==48)
{
for(j=0;j<n;j++)
{
if(A[j]==49)
{
s=abs(B[j]-B[i]);
if(m>s)
{
m=s;
x=j;
}
}
}
o+=abs(B[x]-B[i]);
A[i]=49;
}
}
printf("%lld\n",o);
}
return 0;
}
why i get WA

#include<stdio.h>
int main(){
int t,n;
int a[200];

scanf("%d",&t);
while(t–){
scanf("%d",&n);
int temp=0;
int i=2;
a[0]=1;
int m=1;
int x;
while(i<=n){
int index=0;
while(index<m){
x=a[index]*i+temp;
a[index]=x%10;
temp=x/10;
index++;
}
while(temp!=0){
x=temp%10;
a[index]=x;
temp=temp/10;
m++;
index++;
}
i++;

}
int k;
for( k=m-1;k>=0;k–)
printf("%d",a[k]);

}

}

Why my answers is wrong ??

#include
using namespace std;

int main()
{
int x;
float y;
cin>>x>>y;
if(x%5!=0 || x>=y)
{
cout<<y<<endl;
}

else if(x%5==0)
{
    cout<<y-x-0.5<<endl;
}

return 0;

}
why am i getting wrong answer?

Check this code for this problem - Ice Cream Problem
Working perfectly for sample example - My Solution

why i get wrong answer

#include<stdio.h>
main()
{
int a[100];
int cnt=0,i=0,j=0;
printf(“enter no of digit”);
scanf("%d",&j);
printf(“enter”);
for(i=0;i<=j;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=j;i++)
{
if(a[i]!=42)
printf("%d",a[i]);
else
break;
}
}
what is wrong with this code???

#include <stdio.h>
#include <string.h>

int main(void)
{
int T;
int win = 0;
int lose = 0;
char S[100];

do 
{
    scanf("%d",&T);
}
while ( T < 0 || T > 1000);

for(int j = 0; j < T;j++)
{
scanf("%s",S);

for(int i = 0; i < strlen(S); i++)
{
    if(S[i] != '1')
        ++lose;
    else
        ++win;
}

if (win > lose)
    printf("WIN\n");
else
    printf("LOSE\n");

}
 return 0;   
}

why WA ?

import java.util.*;
class cd2
{
public static void main(String ar[])
{
Scanner s=new Scanner(System.in);
Long T,D;
T=s.nextLong();
while(T–!=0)
{
D=s.nextLong();
Long w,z;
w=(long)0;
z=(long)0;
while(D>0)
{
if(D%10==1)
w++;
else
z++;
D=D/10;
}
if((w==1)||(z==1))
System.out.println(“Yes”);
else
System.out.println(“No”);
}
}
}

It’s giving wrong answer.

I am able to get all the answers for the sample inputs given in the problem. But still I get “Wrong answer”.
my problem code is :: CHEFKEY and my answer code is below .

/*
			-----   Kiamottullah   -----
			       (20/ 10/ 2016)
			All code is written by me .
			You can allways use this code for
			Learning context . keep learning

*/

#include
#include
using namespace std;

class Pair{
private :
long long int X;
long long int Y;

public :
    Pair(int x, int y){
        X = x;
        Y = y;       // constructor
    }
    		// prototypes of this class
    int getX (){
        return X;
    }
    int getY (){
        return Y;
    }
    void show (){
        cout << getX() << " " << getY() << endl;
    }

};

class Testcase{
private :
int N; // height
int M; // width
long long int C; // number of colours

    vector <Pair> Pairs;    // it's pairs
    
public :
    Testcase (int n, int m, int c){
        N = n;
        M = m;      // constructor
        C = c;
        calculatePairs();
    }
    
    		// prototypes of this class
    int getN (){
        return N;
    }
    int getM (){
        return M;
    }
    int getC (){
        return C;
    }
    
    void calculatePairs (){
    // is all color (C) fit in (n*m) display
        if((N * M) >= C){
        
        	// firstly push two pairs (1, C) and (C, 1)
            int x = 1, y = C;
            if(isFit(x, y)){
                Pairs.push_back(Pair(x, y));
            }
            x = C, y = 1;
            if(isFit(x, y)){
                Pairs.push_back(Pair(x, y));
            }
            // checking all the pairs from 2 to C/2
            int hulf = C/2;
            for(int i = 2; i <= hulf; i++){		
                for(int j = 2; j <= C/i; j++){
                    if(isPair(i, j)){
                        if(isFit(i, j)){
                            Pairs.push_back(Pair(i, j));
                        }
                    }
                }
            }
        }else {
			        // so if all color can't drawble
			        // Pair size will be 0
        }
    }
    void showPairs (){
        int s = Pairs.size();	// this function show all the Pairs
        for(int i = 0;i < s; i++){	// of C ... one by one
            Pairs[i].show();
        }
    }
    bool isFit (int x, int y){
        if(((x <= N) && (y <= M))){
            return true;		// this function check whether 
        }else {			// A pair (rect) fit into display (n*m)
            return false;		  
        }
    }
    int getPairsLength (){
        return Pairs.size();	// this function return the number of 
    }				            // Pairs in Pairs vector .
    
    bool isPair (int x, int y){
        if ((x * y) == C){		// this function return true if (x*y) == C
            return true;		// else return false that's it ((x*y) != C)
        }else {
            return false;
        }
    }

};

int main() {
// number of test case;
int T;

// get input of number of test case
cin >> T;

// make an vector of size T;
vector <Testcase> cases;

for(int i = 0; i < T; i++){			// while (i < T) take input of
    int n, m, c;				// (n, m, c) for each testcase
    cin >> n >> m >> c;			// then push them in `cases`
    cases.push_back(Testcase(n, m, c));	// vector
}

for(int i = 0; i < cases.size(); i++){	// for each testcase `tc`
    Testcase tc = cases[i];			// cout the length of Pairs
    cout << tc.getPairsLength() << endl;
}
return 0;

}

One of the best way to handle the wrong ans type problems is that to generate random input by your’s side and then by computer side(By taking random inputs using rand() function in C or C++. It will make you to think more and you will definitely crack the crack the code ASAP…

I am getting the sample output correct but still getting wrong answer. Please help me find my mistake!

#include
using namespace std;
int main()
{ short t,e;
long m;
int k,n,i,j,key,marks;
cin>>t;
int a[10000]={0};
int arr[10001][4]={0};
for(;t>0;t–)
{
cin>>n>>k>>e>>m;
for(j=0;j<n;j++)
{
for(i=0;i<e;i++)
{ if(j==n-1&&i==e-1)
break;
else
{
cin>>arr[j][i];
a[j]+=arr[j][i];
}
}

    }
    
    for(i=1; i<n-1; i++)
    {
        key = a[i];
        j = i-1;
        while(j>=0 && key>a[j])
        {
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = key;
    }
   
    marks=(a[k-1]+1)-a[n-1];
    if(marks<=m)
        if(marks>0)
            cout<<marks;
        else
            cout<<"0";
    else
        cout<<"Impossible ";
    
}
return 0;

}

For the problem- Chef Under Pressure, in the subtask 1, out of 10 tasks, it is showing wrong answer for task 0, rest all are coming correct. I checked for possible corner cases that i might have missed, but couldn’t find anything. Please tell me what is task 0 and its output, so that i can modify my code. Thanks

#include <stdio.h>
#include <stdlib.h>
char a[25];
int top=-1;
void push(char symbol)
{

a[++top]=symbol;

}
char pop()
{

char item;
item=a[top--];
return(item);

}
int precd(char op)
{

int r;
switch(op)
{

case'^':r=3; break;
case'*':
case'/':
case'%':r=2; break;
case'+':
case'-':
case'(':r=0; break;
case'#':r=-1;
 break;
}
return(r);

}
void infix_postfix(char infix[],char postfix[])
{

int i,p=0;
char symbol,item;
push('#');
for(i=0;infix[i]!= '\0';i++)
{

    symbol=infix[i];
    switch(symbol)
    {

        case '(':push(symbol);
        break;
        case ')':item=pop();
         while(item!= '(')
        {

            postfix[p++]=item;
            item=pop();
        }
        break;
        case'+':
            case'-':
            case'*':
            case'/':
            case'%':
            while(precd(a[top])>=precd(symbol));
            {

                item=pop();
                postfix[p++]=item;
            }
            push(symbol);
            break;
            default  : postfix[p++]=symbol;
            break;
    }
}
while(top>0)
{

    item=pop();
    postfix[p++]=item;
}
postfix[p]= '\0';

}
int main()
{
char infix[25],postfix[25];

printf("enter infix expression");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("\npostfix expression:%s\n\n\t",postfix);
return 0;

}

Please put \n at the time of printing your output…
That’s all
like

printf("%d\n",ans); or cout<<ans<<endl;

2 Likes

#include
using namespace std;
int main()
{
long long int t,n1,n2,k,l;
cin>>t;
for (int i=0;i<t;i++)
{
cin>>n1>>n2;
k=n1;
l=n2;
while(n1 != n2)
{
if(n1>n2)
n1-=n2;
else
n2-=n1;
}
cout<<n1<<" "<<(k*l)/n1<<endl;
}
return 0;
}

can someone help me, trying to submit this code but getting wrong answer.Tried another questions many time still not getting it right.This code displays hcf and lcm of two given numbers.
Is there any role of constraints provided?
or some specific way to output the answer?
i’m a beginner please help.

u dont need to use long long int for it.try this code.it is similar to ur code
#include
using namespace std;

int main()
{
int t,n1,n2;
cin>>t;
while(t–){
cin>>n1>>n2;
int x=n1,y=n2;
while(x!=y){
if(x>y)
x-=y;
else
y-=x;
}
cout<<x<<endl<<n1*n2/x<<endl;
}
return 0;

I FORGOT A } AFTER THE RETURN 0;. ADD THAT ALSO