题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <stdio.h>
#include <string.h>
int main() {
char str1[100],str2[100];
scanf("%s",str1);
scanf("%s",str2);
int len1 = strlen(str1);
int len2 = strlen(str2);
char* max_str;
char* min_str;
int len_max = len1>=len2?len1:len2;
if(len_max==len1)
{
max_str = str1;
min_str = str2;
}
else {
max_str = str2;
min_str = str1;
}
int plus =0;
int a,b;
int sum =0;
int reverse[1000] = {0};
int count=0;
for(int j=0;j<strlen(min_str);j++)
{
a = max_str[len_max-1-j]-'0';
b = min_str[strlen(min_str)-1-j]-'0';
sum = a+b+plus;
plus =sum/10;
sum = sum%10;
reverse[count++] = sum;
}
for(int j=strlen(max_str)-strlen(min_str)-1;j>=0;j--)
{
sum = max_str[j]-'0'+plus;
plus =sum/10;
sum = sum%10;
reverse[count++] = sum;
}
if(plus>0)
{
reverse[count++] = plus;
}
for(int i=count-1;i>=0;i--)
{
printf("%d",reverse[i]);
}
return 0;
}