Getting WA on testcase 8
testcase 8 is
981
1
and the answer is "YES"
my code is given the write answer.But why???
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define diff(x,y) x>y ? (x-y):(y-x)
#define i64d unsigned long long
int main(){
int n,j,i,k,t,l,s;
char a[10],b[10],c[10],d[10],e[10],f[10];
scanf("%d %d",&n,&k);
t=n+k;
sprintf(a,"%d",n);
sprintf(b,"%d",k);
sprintf(e,"%d",t);
j=0;
for(i=0;i<strlen(e);i++){
if(e[i]!='0')
f[j++]=e[i];
}
j=0;
for(i=0;i<strlen(a);i++){
if(a[i]!='0')
c[j++]=a[i];
}
j=0;
for(i=0;i<strlen(b);i++){
if(b[i]!='0')
d[j++]=b[i];
}
s=atoi(c);
l=atoi(d);
k=atoi(f);
if(s+l==k)
printf("YES\n");
else
printf("NO\n");
return 0;
}
testcase 8 is
981
1
and the answer is "YES"
my code is given the write answer.But why???
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
#define diff(x,y) x>y ? (x-y):(y-x)
#define i64d unsigned long long
int main(){
int n,j,i,k,t,l,s;
char a[10],b[10],c[10],d[10],e[10],f[10];
scanf("%d %d",&n,&k);
t=n+k;
sprintf(a,"%d",n);
sprintf(b,"%d",k);
sprintf(e,"%d",t);
j=0;
for(i=0;i<strlen(e);i++){
if(e[i]!='0')
f[j++]=e[i];
}
j=0;
for(i=0;i<strlen(a);i++){
if(a[i]!='0')
c[j++]=a[i];
}
j=0;
for(i=0;i<strlen(b);i++){
if(b[i]!='0')
d[j++]=b[i];
}
s=atoi(c);
l=atoi(d);
k=atoi(f);
if(s+l==k)
printf("YES\n");
else
printf("NO\n");
return 0;
}
your char arrays should be initialized.
try:
1. use memset. (or)
2. declare them as global variables. (or)
3. change the "<" to "<=" in the for's to them copy the '\0';