## 713 - Adding Reversed Numbers

Moderator: Board moderators

iftekherbd
New poster
Posts: 1
Joined: Fri Jun 21, 2013 5:24 pm

I submit the problem 713. my compiler show correct ans of this problem. but when i submitted it uva judge then its show wrong ans please tel me whats wrong with me. please see this code =>

#include <stdio.h>

int rev (int number);
int rev1 (int number);
int rev2 (int number);
int rev(int number) {
int res = 0;
while (number > 0) {
res = res * 10 + number%10;
number /= 10;
}
return res;
}
int rev1(int number) {
int res = 0;
while (number > 0) {
res = res * 10 + number%10;
number /= 10;
}
return res;
}
int rev2(int number) {
int res = 0;
while (number > 0) {
res = res * 10 + number%10;
number /= 10;
}
return res;
}

int main ()
{
int a,b,c,d,e,f,i,j;
scanf ("%d",&j);
for (i=1;i<=j;i++){
scanf ("%d %d",&a,&b);
c=rev(a);
d=rev(b);
e=c+d;
f=rev(e);

printf ("%d\n",f);}
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Numbers will be at most 200 characters long.
Check input and AC output for thousands of problems on uDebug!

hungphongbk
New poster
Posts: 2
Joined: Sat Oct 12, 2013 6:01 pm

sorry but anyone can help me ( I can't understand why I always get WA, I tried 5 times to submit

Code: Select all

``````import java.io.*;
import java.lang.String;
import java.math.BigInteger;
import java.util.*;

class Main
{
static String a,b;
static StringTokenizer st;

static String ReadLn (int maxLg)  // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try
{
while (lg < maxLg)
{
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}

if ((car < 0) && (lg == 0)) return (null);  // eof
return (new String (lin, 0, lg));
}
{
String s="",s2;
do
{
if (s2==null) break;
s=s+s2+" ";
}
while (true);
st=new StringTokenizer(s);
}

{
return Integer.parseInt(st.nextToken());
}
{
String s=st.nextToken();
s=(new StringBuilder(s)).reverse().toString();
return new BigInteger(s);
}
static BigInteger reverse(BigInteger x)
{
String s=x.toString();
s=(new StringBuilder(s)).reverse().toString();
return new BigInteger(s);
}
public static void main(String args[])
{
int n=Integer.parseInt(s.nextToken());
BigInteger a,b;
boolean begin=true;
for (int i=0;i<n;i++)
{

//if (begin) begin=false;
//else System.out.println();
}
}
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

That code won't compile.
Check input and AC output for thousands of problems on uDebug!

anup10.duet
New poster
Posts: 8
Joined: Thu Jun 27, 2013 9:21 am

### 713 Adding Reversed Numbers why got WA? help me.......

Code: Select all

``````#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int testcase,flag;
string num1,num2,result,temp,temp2;
char temp1;
cin>>testcase;
while(testcase != 0)
{
cin>>num1>>num2;
int carry,sum,length1,length2,count1,digit,index;
length1 = num1.length();
length2 = num2.length();
count1  = 1;
index = carry = sum = digit = flag = 0;
result = "\0";
if(length1 >= length2)
{
while(count1 <= length2)
{
sum = num1[index] - '0' + num2[index] - '0' + carry;
if(sum < 10)
{
digit = sum;
carry = 0;
if(result.length() == 0 && digit > 0)
{
temp1 = digit + 48;
temp = temp1;
result = temp;
}
else if(result.length() != 0)
{
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}

}
else
{
carry = sum / 10;
digit = sum % 10;
if(result.length() == 0 && digit > 0)
{
temp1 = digit + 48;
temp = temp1;
result = temp;
}
else if(result.length() != 0)
{
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}
}
count1++;
index++;
}
while(count1 <= length1)
{

sum = num1[index] - '0' + carry;
if(sum < 10)
{
digit = sum;
carry = 0;
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}
else
{
carry = sum / 10;
digit = sum % 10;
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}
count1++;
index++;
}
}
else
{
while(count1 <=length1)
{
sum = num1[index] - '0' + num2[index] - '0' + carry;
if(sum < 10)
{
digit = sum;
carry = 0;
if(result.length() == 0 && digit > 0)
{
temp1 = digit + 48;
temp = temp1;
result = temp;
}
else if(result.length() != 0)
{
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}
}
else
{
carry = sum / 10;
digit = sum % 10;
if(result.length() == 0 && digit > 0)
{
temp1 = digit + 48;
temp = temp1;
result = temp;
}
else if(result.length() != 0)
{
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}
}
count1++;
index++;
}
while(count1 <= length2)
{
sum = num2[index] - '0' + carry;
if(sum < 10)
{
digit = sum;
carry = 0;
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}
else
{
carry = sum / 10;
digit = sum % 10;
temp1 = digit + 48;
temp = temp1;
result.append(temp);
}
count1++;
index++;
}
}
if(carry != 0)
{
carry = carry + 48;
temp = carry;
result.append(temp);
}
cout<<result<<endl;
testcase--;
}
return 0;
}

``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 713 Adding Reversed Numbers why got WA? help me.......

Check input and AC output for thousands of problems on uDebug!

anup10.duet
New poster
Posts: 8
Joined: Thu Jun 27, 2013 9:21 am

### Re: 713 Adding Reversed Numbers why got WA? help me.......

I saw the topic,also check some critical input.But I don't find any problem and still I got WA...

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 713 Adding Reversed Numbers why got WA? help me.......

brianfry713 wrote:

Code: Select all

``````100
18846 29
3 539
6371812887 345499277
895 6255126497
323 22138854
75963 8577219978
5 59
42 7671811
6 38
451162048 5822434
918 5514665422
870976 721077626
1831 89257874
42932 890117171
24411086 8023
71 9386
18487587 257886
9331576856 910180694
1046735648 29948532
64067 1229519273
75201 73
53 485002
25 727832
499 94
171 3496278311
46486 65
94333 12752698
11192 18616242
25679 28743229
3997054 959372
198499085 833
8 594
64 904933464
98556 77997
6870991406 1189665
714 21
1 8108
9519979126 9725
1257165 10100626
5141 870201
143 5
78837847 903
7609876 7916179774
919505334 2502797
13666472 2383106
9 8
21 7633759
52 92137772
4 87790227
7564446156 88688
75072776 52056129
60407155 4291
21868 984102927
2 2643719
910629 53
137567956 638
6 19835
623786 3112
420791216 674
7919687 102836114
368486 569297
119691679 667193345
204571596 9059670564
61382 89
145 83
180582093 41
61277 8515541834
9362 77054
9360485 6862316492
84159114 4622986
136714042 7
8 762383461
674 26761557
38888706 5916465
1591791 611239
71021 85
201 6073586
65246477 49
5804 2549216409
6766293954 938
4931121 232
22393083 1454688979
7580870209 26791798
297477292 3625805
74304 84263696
155604365 879010655
53 441912
3 151016646
94 1749
111 12185406
2 47
798562457 46
21424 483008
22 66
997 85
917783038 98
133656 7
6485588 2824824
97110568 925
486 463844556``````
AC output:

Code: Select all

``````37946
839
9726715568
4216126497
54438854
5174619978
1
1971811
98
934306448
4794665422
502944726
97667874
220537171
5641086
6586
33276297
8532382807
3931698848
7625329273
49201
916002
977832
3401
4117278311
2586
7095698
29709242
43423329
2591874
922599085
305
554933464
665541
7950667406
924
9108
8344089126
22671286
394301
643
69147847
4625056874
179774144
36408433
71
9733759
45137772
28790227
5433356156
280298951
3327155
103882927
4643719
450629
765667956
79835
934986
5791216
893705914
8287741
7768859151
1194495435
41482
975
590582093
4732351834
61774
5233791592
21478084
836714042
572383461
83271557
87053466
7604092
57021
8083586
5346477
7353316409
5157293954
6261121
3683096389
9160050119
550068792
59567696
9257149111
971912
451016646
259
23285406
67
169562457
697248
88
758
808783038
833656
82104131
80710568
840944556``````
Check input and AC output for thousands of problems on uDebug!

anup10.duet
New poster
Posts: 8
Joined: Thu Jun 27, 2013 9:21 am

### Re: 713 Adding Reversed Numbers why got WA? help me.......

thanks to brianfry713.

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

### Re: 713 - Adding Reversed Numbers

gettng wa...help me

Code: Select all

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

int main()

{
int j,t,k,x,i,len,len1,q,l;
char s[2002],s1[2002],str[2002]={0},str1[2001]={0},sum[2002],sum1[1000];

scanf("%d",&t);
getchar();

for(x=0; x<t; x++)

{

scanf("%s%s",s,s1);
k=0;
i=0;
q=0;
len=strlen(s);
len1=strlen(s1);

if(len>len1)
len1=len;

for(j=0; j<len1; j++)
{
if(s[j]>='0'&&s[j]<='9')
str[i]=s[j];
else
str[i]='0';
i++;
if(s1[j]>='0'&&s1[j]<='9')
str1[k]=s1[j];

else
str1[k]='0';
k++;
}

str[i]='\0';str1[k]='\0';

int z=0;
int carry=0,m;
for(i=0; i<strlen(str); i++)
{

m=str[i]-48+str1[i]-48+carry;
carry=m/10;
if(m>=10)
{sum[z]=m%10+'0';
if(i==strlen(str)-1)
sum[++z]=carry+48;
}
else
sum[z]=m+'0';

z++;

}

if(sum[0]=='0')
{
for(l=0;l<z;l++)
if(sum[l]!='0')
break;
for(m=l;m<z;m++)
{
sum1[q]=sum[m];
q++;
}
sum1[q]='\0';
printf("%s\n",sum1);
}

else
printf("%s\n",sum);
}
return 0;
}
/code]``````

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

### Re: 713 - Adding Reversed Numbers

getting wrong answer i try several input...help me

Code: Select all

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

int main()

{
int j,t,k,x,i,len,len1,q,l;
char s[2002],s1[2002],str[2002]= {0},str1[2001]= {0},sum[2002],sum1[1000];

scanf("%d",&t);
getchar();

for(x=0; x<t; x++)

{

scanf("%s%s",s,s1);
k=0;
i=0;
q=0;
len=strlen(s);
len1=strlen(s1);

if(len>len1)
len1=len;

for(j=0; j<len1; j++)
{
if(s[j]>='0'&&s[j]<='9')
str[i]=s[j];
else
str[i]='0';
i++;
if(s1[j]>='0'&&s1[j]<='9')
str1[k]=s1[j];

else
str1[k]='0';
k++;
}

str[i]='\0';
str1[k]='\0';

int z=0;
int carry=0,m;
for(i=0; i<strlen(str); i++)
{

m=str[i]-48+str1[i]-48+carry;
carry=m/10;
if(m>=10)
{
sum[z]=m%10+'0';
if(i==strlen(str)-1)
{
z++;
sum[z]=carry+48;

}
}
else
{

sum[z]=m+'0';
}
z++;

}

sum[z]='\0';

if(sum[0]=='0')
{
for(l=0; l<z; l++)
if(sum[l]!='0')
break;
for(m=l; m<z; m++)
{
sum1[q]=sum[m];
q++;
}
sum1[q]='\0';
printf("%s\n",sum1);
}

else
printf("%s\n",sum);
}
return 0;
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 713 - Adding Reversed Numbers

Input:

Code: Select all

``````2
999999 999999
1 999999
``````
AC output:

Code: Select all

``````8999991
1
``````
Check input and AC output for thousands of problems on uDebug!

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

### Re: 713 - Adding Reversed Numbers

mentalist_mbstu wrote:getting wrong answer i try several input...help me

Code: Select all

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

int main()

{
int j,t,k,x,i,len,len1,q,l;
char s[2002],s1[2002],str[2002]= {0},str1[2001]= {0},sum[2002],sum1[1000];

scanf("%d",&t);
getchar();

for(x=0; x<t; x++)

{

scanf("%s%s",s,s1);
k=0;
i=0;
q=0;
len=strlen(s);
len1=strlen(s1);

if(len>len1)
len1=len;

for(j=0; j<len1; j++)
{
if(s[j]>='0'&&s[j]<='9')
str[i]=s[j];
else
str[i]='0';
i++;
if(s1[j]>='0'&&s1[j]<='9')
str1[k]=s1[j];

else
str1[k]='0';
k++;
}

str[i]='\0';
str1[k]='\0';

int z=0;
int carry=0,m;
for(i=0; i<strlen(str); i++)
{

m=str[i]-48+str1[i]-48+carry;
carry=m/10;
if(m>=10)
{
sum[z]=m%10+'0';
if(i==strlen(str)-1)
{
z++;
sum[z]=carry+48;

}
}
else
{

sum[z]=m+'0';
}
z++;

}

sum[z]='\0';

if(sum[0]=='0')
{
for(l=0; l<z; l++)
if(sum[l]!='0')
break;
for(m=l; m<z; m++)
{
sum1[q]=sum[m];
q++;
}
sum1[q[quote]i don't understand when i input  those different number output shows different [/quote]]='\0';
[code]input
2
1 999999
999999 999999
output
1
8999991
input
2
999999 999999
1 999999
``````
printf("%s\n",sum1);
}

else
printf("%s\n",sum);
}
return 0;
}
[/code]

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

### Re: 713 - Adding Reversed Numbers

mentalist_mbstu wrote:
mentalist_mbstu wrote:getting wrong answer i try several input...help me

Code: Select all

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

int main()

{
int j,t,k,x,i,len,len1,q,l;
char s[2002],s1[2002],str[2002]= {0},str1[2001]= {0},sum[2002],sum1[1000];

scanf("%d",&t);
getchar();

for(x=0; x<t; x++)

{

scanf("%s%s",s,s1);
k=0;
i=0;
q=0;
len=strlen(s);
len1=strlen(s1);

if(len>len1)
len1=len;

for(j=0; j<len1; j++)
{
if(s[j]>='0'&&s[j]<='9')
str[i]=s[j];
else
str[i]='0';
i++;
if(s1[j]>='0'&&s1[j]<='9')
str1[k]=s1[j];

else
str1[k]='0';
k++;
}

str[i]='\0';
str1[k]='\0';

int z=0;
int carry=0,m;
for(i=0; i<strlen(str); i++)
{

m=str[i]-48+str1[i]-48+carry;
carry=m/10;
if(m>=10)
{
sum[z]=m%10+'0';
if(i==strlen(str)-1)
{
z++;
sum[z]=carry+48;

}
}
else
{

sum[z]=m+'0';
}
z++;

}

sum[z]='\0';

if(sum[0]=='0')
{
for(l=0; l<z; l++)
if(sum[l]!='0')
break;
for(m=l; m<z; m++)
{
sum1[q]=sum[m];
q++;
}

printf("%s\n",sum1);
}

else
printf("%s\n",sum);
}
return 0;
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 713 - Adding Reversed Numbers

On my machine your code doesn't match the sample input. It is printing some extra characters on the third line.
Check input and AC output for thousands of problems on uDebug!