题目描述
Now,we know that Yuanfang is the best student in the class. Every time when the teacher comes up with a hard problem and it seems no student can solve it, He always says: "Yuanfang , what do you think?".
Today, They had another math class,(why always math class?)The teacher said:”Let’s calculate the sum of A+B,but there are something different ,the two integers A and B are very very large,Yuanfang, what do you think?"
Yuanfang wanted to solve this problem quickly. So he came to you again, the only programmer he knows, for help.
You should write a program to calculate A+B to help him.
输入
There are several test cases.
Each case has two integers A and B (0≤A,B≤10^60)in a line.
Each case has two integers A and B (0≤A,B≤10^60)in a line.
输出
For each test case,output one line containing a result A+B.
示例输入
10000000000000000000001 10000000000000000000001
示例输出
20000000000000000000002
高精度问题
#include<stdio.h>
#include<string.h>
int main()
{
char st1[100],st2[100];
int a[100],b[100],n1,n2,k,i,j;
while(~scanf("%s %s",st1,st2))
{
memset(a,0,sizeof(a));//初始化为0
memset(b,0,sizeof(b));//同上
n1=strlen(st1);
n2=strlen(st2);
#include<string.h>
int main()
{
char st1[100],st2[100];
int a[100],b[100],n1,n2,k,i,j;
while(~scanf("%s %s",st1,st2))
{
memset(a,0,sizeof(a));//初始化为0
memset(b,0,sizeof(b));//同上
n1=strlen(st1);
n2=strlen(st2);
if(n1==1&&n2==1&&st1[0]=='0'&&st2[0]=='0')
printf("0\n");
for(i=0;i<n1;i++)
a[n1-1-i]=st1[i]-'0';
for(i=0;i<n2;i++)
b[n2-1-i]=st2[i]-'0';
k=n1;
if(n1<n2)
k=n2;
for(i=0;i<k;i++)
a[i]=a[i]+b[i];
for(i=0;i<k;i++)
{
if(a[i]>9) //进位
{
a[i+1]=a[i+1]+a[i]/10;
a[i]=a[i]%10;
}
else
a[i]=a[i];
}
for(j=k;j>=0;j--)
{
if(a[j])break;//如果a[j]==0,跳出,返回j值。用来排除前导0;
}
for(i=j;i>=0;i--)
{
printf("%d",a[i]);
}
printf("\n");
}
}
for(i=0;i<n1;i++)
a[n1-1-i]=st1[i]-'0';
for(i=0;i<n2;i++)
b[n2-1-i]=st2[i]-'0';
k=n1;
if(n1<n2)
k=n2;
for(i=0;i<k;i++)
a[i]=a[i]+b[i];
for(i=0;i<k;i++)
{
if(a[i]>9) //进位
{
a[i+1]=a[i+1]+a[i]/10;
a[i]=a[i]%10;
}
else
a[i]=a[i];
}
for(j=k;j>=0;j--)
{
if(a[j])break;//如果a[j]==0,跳出,返回j值。用来排除前导0;
}
for(i=j;i>=0;i--)
{
printf("%d",a[i]);
}
printf("\n");
}
}