Why do I get Wrong Answer?

,

#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

Hi, i allways get wrong answer in my codes, even tho im getting the correct answer in various tests.

can someone help me?

this is an example: (Using C)

#include <stdio.h>
#include <stdlib.h>

int main()
{
int maxA,maxB,i,n,A[10000],B[10000],C[10000],D[10000],sum_A=0,sum_B=0;
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&A[i]);
scanf("%d",&B[i]);
C[i]=A[i]-B[i];
D[i]=B[i]-A[i];
//sum_A+=A[i];
//sum_B+=B[i];
maxA=C[i];
maxB=D[i];
}
for (i=1;i<n;i++)
{
if (C[i]>C[(i-1)] && C[i]>maxA)
{
maxA=C[i];
}
else if (C[(i-1)]>C[i] && C[(i-1)]>maxA)
{
maxA=C[(i-1)];
}
if (D[i]>D[(i-1)] && D[i]>maxB)
{
maxB=D[i];

}
    else if (D[(i-1)]>D[i] && D[(i-1)]>maxB)
         {
             maxB=D[(i-1)];
         }

}

if (maxA>maxB)
{
printf("%d %d \n",1,maxA);
}
else
{
printf("%d %d \n",2,maxB);
}
return 0;
}

Contest Code: Practice
Problem code: NUMGAME2
My solution:
#include <bits/stdc++.h>
#define pd(x) printf("%d",x)
#define sld(x) scanf("%ld",&x)
#define plld(x) printf("%lld",x)
#define slld(x) scanf("%lld",&x)
using namespace std;
long long int t;
int isprime (long long int);
int main()
{
long long int n,x,k,q;
slld(t);
while(t–)
{ k=0;
slld(n);
q=n-1;
while(q>=1)
{
if((q==1||isprime(q))&&(n-q)>0)
{
n-=q;
k++;
q=n;
continue;
}
q–;
}
if(k%2==0)
printf(“ALICE\n”);
else
printf(“BOB\n”);
}
return 0;
}
int isprime( long long int N)
{
if(N<2 || (!(N&1) && N!=2))
return 0;
for(int i=3; i*i<=N; i+=2){
if(!(N%i))
return 0;
}
return 1;
}

Plz help to debug my code.

it show wrong ans of matrix transformation

just check out that your program is following the constraints given in the problem.
try to take input just as the given in constraints. This will help you to determine whether your logic is correct or not.
Hope this helps you!!

2 Likes

#include

using namespace std;

int main(){
int t=0,n=0,i=1,j=0,ans=1 ;
cin >> t ;
while(i<=t){
cin >> n ;
for(j=n;j>=1;j–){
ans *=j ;
}
cout << ans << endl;
ans = 1;
i++ ;
}
return 0;
}

THIS IS SHOWING WRONG ANSWER . WHAT IS THE PROBLEM?

#include

using namespace std ;

int main(){
int i=1,ans,p1,p2,n,lead=0;
cin >> n;
while(i<=n){
cin >> p1 >> p2 ;
lead = lead + p1-p2 ;
i++ ;
}
if(lead>0){
cout << “1” << " " << lead << endl;
}
else{
ans= lead * (-1) ;
cout << “2” << " " << ans << endl;
}
return 0;
}

why is this giving wrong answer?
the url for this problem is TLG Problem - CodeChef

#include

using namespace std ;

int main(){
int i=1,ans,p1,p2,n,lead=0;
cin >> n;
while(i<=n){
cin >> p1 >> p2 ;
lead = lead + p1-p2 ;
i++ ;
}
if(lead>0){
cout << “1” << " " << lead << endl;
}
else{
ans= lead * (-1) ;
cout << “2” << " " << ans << endl;
}
return 0;
}

why am i getting wrong answer?

@eshan292

I think you misinterpreted the Q.

You have to find the MAXIMUM difference in scores, and the player with the lead wins.

In your case, "lead = lead + p1-p2 " is taking into account the previous lead/lead in previous round, while the example clearly showed this is not the case.

I advice, please give another attempt in understanding the Q, and if you’re still facing problem, look at its solution here

Hi. I am using System.out.println in java to print my answer. Would it cause new line trouble, giving WA? All my answers seem correct with given test cases and few more that I generated by my self.

#include

using namespace std;

int main()
{
int t,n,i=0,j=0,a,b,c=0,sum=0,d=0,ctr=0,e,temp ;
cin >> t ;
while(i<t){
cin >> n ;
int arr[n] ;
j=0 ;
while(j<n){
cin >> arr[j] ;
j++ ;
}
c=0 ;
sum=0 ;
while(c<n){
sum=sum+arr[c] ;
c++ ;
}
for(e=0;e<1;e++){
if(sum!=n*((n+1)/2)){
cout << “no” << endl;
temp=1 ;
break ;
}
ctr=0 ;
d=0;
while(d<n-1){
if(arr[d]+1==arr[d+1]){
ctr+=1 ;
}
d++ ;
}
if(ctr==n-1){
cout << “no” << endl ;
temp=1 ;
break ;
}
for(b=0;b<n;b++){
for(a=1;a<n;a++){
if(arr[b]==arr[a] && b!=a){
cout << “no” << endl;
temp=1;
break ;
}
}
if(temp==1){
break;
}

    }
    }
    if(temp!=1){
        cout << "yes" << endl;
    }
    i++ ;
}
return 0;

}
why am i getting wrong answer?
CodeChef: Practical coding for everyone>url for the problem

this is code of life,the universe and everything.
#include<stdio.h>
int main()
{
int a;

while(1)
{
scanf("%d\n",&a);
if(a==42)
break;
}
return 0;
}

Why is my program getting wrong answer?

import java.util.*;

class pattern {
public static void main(String argsp[]){
int q;
Scanner in=new Scanner(System.in);
int a,b,j,c,d;
q=in.nextInt();
for(int i=1;i<=q;i++){
a=in.nextInt();
b=in.nextInt();
if(a<1||b<1||a==b)
System.out.println(“No”);
else if(a==(b+2)||a==(b-2))
System.out.println(“Yes”);
else
{
j=(a-1)/2;
c=2j+1;
d=2
j+2;
if((c==a&&d==b)||(c==b&&d==a))
System.out.println(“Yes”);
else
System.out.println(“No”);
}
}
}
}

def remove_dot(this_list,value):
while value in this_list:
this_list.remove(value)

def check(list2):
if(list2==[]):
return “Valid”
elif(list2[0]==‘T’ or list2[len(list2)-1]==‘H’):
return “Invalid”
else :
count=0
for i in range(0,len(list2)-1,2):
if(list2[i] is ‘H’ and list2[i+1] is ‘T’):
count+=1
if((count*2) is len(list2)):
return “Valid”
else :
return “Invalid”

R=int(input())
L=[]
str=[]
dot=’.’
ans=[]
for i in range(0,R):
L.append(int(input()))
#for j in range(0,L[i]):
value=input()
list=[]
list[:0]=value
remove_dot(list,dot)
ans.append(check(list))
for i in range(0,R):
print(ans[i])

This is my python code i run in my machine it gives me right output but when i submit in SmackDown contest it tells wrong answer i am trying from 2 days

beginners first problem answer is getting wrong…please tell me where am I wrong?

#include<stdio.h>
#include<string.h>
int main()
{
int i,j,t,s,m;
scanf("%d",&t);
for(i=0;i<t;i++)
{
s=0;
m=0;
char str[100];
scanf("%s",str);
for(j=0;str[j]!=’\0’;j++)
{if(str[j]==‘s’)
s+=1;
else if(str[j]==‘m’) m+=1;
}
for(j=0;j!=(strlen(str)-1);j++)
{
if(str[j]==‘s’&&str[j+1]==‘m’)
{
s-=1;
str[j]=‘a’;}
else if(str[j]==‘m’&&str[j+1]==‘s’)
{
s-=1;
str[j+1]=‘a’;
}

}
if(m>s)
printf(“mongooses\n”);
else if(s>m)
printf(“snakes\n”);
else
printf(“tie\n”);

}
return 0;
}