I think I couldn’t get the problems constraint right, although I partially solved the problem, got 30 pts. But I am not sure what is the logical error here. Any java wizard here to help?
The problem doesnt mention it in the statement but when outputting the answer we have to remove leading zero’s which is not done in this code
You should have read the comments .
test case 10001 90009
correct ans : 0 not 00000
The problem was with the leading zeros . I too solved the problem but didn’t knew that leading zeros needs to be removed.
DO CHECK COMMENT SECTION DURING CONTESTS !! IT HELPS.
#include
#include
using namespace std;
int main(void)
{
long int i,j,t,k;
cin>>t;
while(t–)
{
int count1=0;
long int count=0;
char s[10001];
char s1[10001];
char ans[10001];
cin>>s;
cin>>s1;
long int l=strlen(s);
long int l1=strlen(s1);
int temp=0;
//int max1=min(l,l1);
i=l-1,j=l1-1;
k=0;
if(l==l1)
{
temp=1;
while(i>=0 && j>=0)
{
int d=(s[i]-‘0’)+(s1[j]-‘0’);
int x=d%10;
ans[k]=(char)(x+‘0’);
i–;
j–;
k++;
}
ans[k]=’\0’;
//for(i=k-1;i>=0;i–)
//cout<<ans[i];
//continue;
}
else if(l>l1)
{
int f=l-l1;
for(i=0,j=0,k=0;i<l && j<l1 && k<l;i++,k++)
{
//count++;
if(count!=f)
{ans[k]=s[i];
count++;
}
else
{int d=(s[i]-‘0’)+(s1[j]-‘0’);
int x=d%10;
ans[k]=(char)(x+‘0’);
j++;
}
//count++;
}
ans[k]=’\0’;
//cout<<ans;
}
else if(l1>l)
{
int f=l1-l;
for(i=0,j=0,k=0;i<l && j<l1 && k<l1;j++,k++)
{
//count++;
if(count!=f)
{ans[k]=s1[j];
count++;
}
else
{int d=(s[i]-'0')+(s1[j]-'0');
int x=d%10;
ans[k]=(char)(x+'0');
i++;
}
//count++;
}
ans[k]='\0';
//cout<<ans;
}
for(i=0;i<k;i++)
{
if(ans[i]=='0')
count1++;
}
if(count1==k)
cout<<0;
else if(temp==1)
{
for(i=k-1;i>=0;i--)
cout<<ans[i];
}
else
cout<<ans;
cout<<endl;
}
return 0;
}
Hey can anyone tell where my code is producing the wrong ans
Here is the link to my code.please tell at what test case it is producing the wrong output
44 64
for this it’s showing:08
it’s should show:8
Hope it helps…
To answer in simple way…when two inputs like 555,555 are given,your code prints 000.Actually it is expected to print 0. That’s the problem.
Thanks both of u.I have corrected my code and got AC(100 points).