#include<stdio.h>
#include<string.h>
void main(void)
{
int i = 0 ;
int j = 0 ;
int end = 0 ;
int inext = 0 ;
int EndStr = 0 ;
char str1[80] ;
char str2[80] ;
char *pstr ;
gets(str1) ;
gets(str2) ;
i = strlen(str1) ;
j = strlen(str2) ;
if(i >= j)
{
end = j - 1 ;
pstr = str1 ;
EndStr = i - 1 ;
}
else
{
end = i - 1 ;
pstr = str2 ;
EndStr = j - 1 ;
}
for( ; end >= 0 ; end-- ,i--,j-- ,EndStr--)
{
if(str1[i-1] + str2[j-1] + inext - '0' >= 58)
{
pstr[EndStr] = str1[i-1] + str2[j-1] + inext - 58 ;
inext = 1 ;
}
else
{
pstr[EndStr] = str1[i-1] + str2[j-1] + inext - '0' ;
inext = 0 ;
}
}
if(1 == inext)
{
for(i = EndStr ; i >= 0 ; i--)
{
if(pstr[i] + inext >= 58)
{
pstr[i] = pstr[i] + inext - 58 + '0' ;
inext = 1 ;
}
else
{
pstr[i] = pstr[i] + 1 ;
inext = 0 ;
break ;
}
}
if(-1 == i)
{
for(i = strlen(pstr) - 1 , end = i+1, pstr[i+2] = '/0' ; i >= 0 ; i--)
{
pstr[i+1] = pstr[i] ;
}
pstr[0] = '1' ;
pstr[end+1] = '\0' ; //结束
}
}
puts(pstr) ;
}
计算高精度加法
Time Limit:1000MS Memory Limit:65536K
Total Submit:2100 Accepted:1217
Language: not limited
Description
由键盘输入两个位数很长的整数(一行一个,最多不超过80位),试计算并输出这两个数的和。
Input
Output
Sample Input
1234567890123456789353534532453453453434534 987654321098765324534534534534532
Sample Output
1234567891111111110452299856987987987969066
Hint
注意,一个普通的变量不能保存十多位长的整数
Source
Provider
checkie