I searched everything I tried, but I couldn't find an answer I need.
I was solving a problem called "Big Number A+B", with C language. The maximum value of A and B is 10^10000 so I couldn't solve it with unsigned long long
, so I tried using char
array to add it.
#include <stdio.h>
int main(void) {
char tem1[10001], tem2[10001];
char a[10001], b[10001], res[10002] = "";
int carry = 0, sum = 0;
scanf("%s%s", tem1, tem2);
sprintf(a, "%010000s", tem1);
sprintf(b, "%010000s", tem2);
for (int i = 9999; i >= 0; i--) {
sum = (a[i] - 48) + (b[i] - 48) + carry;
carry = sum / 10;
sum = sum % 10;
res[i + 1] = sum + 48;
}
res[0] = carry + 48;
int start = 0;
while (res[start] == 48) {
start++;
}
puts(res + start);
return 0;
}
I declared 5 char
arrays, two for save number temporary, two for numbers for adding, but I put zeros at front to make length exactly 10000, and last for to save result.
Because input was two integers at single line, I used scanf
to read them. And whith sprintf
, I put zeros to make length to 10000. Then inside for
loop, I added a[i]
, b[i]
, and carry
, and mod 10 to save char to array res
. and after breaking out of the loop, I copied last carry to res[0]
.
Actual add is done, but there could be zeros at front of result, so I found where character wasn't '0', and printed it from there.
I put numbers to this program, and everything worked fine. But when I submited this code to judge, it says Output Limit Exceeded, why does it?
I'm sorry if my english was bad, or question was too stupid. Thank you.