Created
December 3, 2017 15:27
-
-
Save henrybear327/318cfe78db603547982f8a99e73c8c24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define N 210 | |
int main() | |
{ | |
char s1[N], s2[N], s3[N]; | |
int ncase, i, length1, length2, maxlength; | |
freopen("string_addReverseNumber.in", "r", stdin); | |
// freopen("my.out","w",stdout); | |
scanf("%d", &ncase); | |
while (ncase--) { | |
scanf("%s%s", s1, s2); | |
length1 = strlen(s1); | |
length2 = strlen(s2); | |
for (i = 0; i < N; i++) // initialize s3 with 0 | |
s3[i] = 0; | |
if (length1 > length2) { | |
for (i = length2; i < length1; i++) { | |
s2[i] = '0'; | |
} | |
s2[length1] = '\0'; | |
maxlength = length1; | |
} else { | |
for (i = length1; i < length2; i++) { | |
s1[i] = '0'; | |
} | |
s1[length2] = '\0'; | |
maxlength = length2; | |
} | |
for (i = 0; i < maxlength; i++) { | |
s3[i] = ((s1[i] - '0') + (s2[i] - '0')); | |
} | |
for (i = 0; i < maxlength; i++) { | |
if (s3[i] > 9) { | |
s3[i + 1] += 1; | |
s3[i] %= 10; | |
} | |
s3[i] += '0'; // do int to char | |
} | |
if (s3[maxlength] != 0) { // don't forget the last carry, e.g. 9999 + 9999 | |
// -> you will have 5 digits!! | |
s3[maxlength] += '0'; | |
maxlength++; | |
s3[maxlength] = '\0'; | |
} | |
/* | |
s3[maxlength] = 0; | |
if (s3[maxlength - 1] > 9) { | |
s3[maxlength] += 1; | |
s3[maxlength - 1] %= 10; | |
s3[maxlength + 1] = '\0'; | |
for (i = 0; i <= maxlength; i++) { | |
s3[i] += '0'; | |
} | |
} else { | |
s3[maxlength] = '\0'; | |
for (i = 0; i < maxlength; i++) | |
s3[i] += '0'; | |
} | |
*/ | |
// find the starting point! | |
int start = 0; | |
for (i = 0; i < N; i++) { | |
if (s3[i] == '0' || s3[i] == '\0') | |
continue; | |
start = i; | |
break; | |
} | |
printf("%s\n", s3 + start); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment