Resource limit
Time limit: 1.0s Memory limit: 512.0MB
Problem Description
Input two integers a and b , and output the sum of these two integers. Both a and b do not exceed 100 digits.
Algorithm Description
Since both a and b are relatively large, they cannot be stored directly using standard data types in the language. For this kind of problem, generally use an array to deal with.
Define an array A , A [0] for storing a th bit, A [. 1] for storing a ten, and so on. You can also use an array B to store b .
When calculating c = a + b , first add A [0] and B [0]. If a carry occurs, store the carry (that is, the tens digit of the sum ) into r , and store the single digit of the sum Enter C [0], that is, C [0] is equal to ( A [0] + B [0])% 10. Then calculate the addition of A [1] and B [1]. At this time, the value r from the low order should also be added up, that is, C[1] should be the sum of A [1], B [1] and r . If another carry occurs, the new carry can still be stored in r , and the ones digit of the sum can be stored in C [1]. By analogy, all bits of C can be found .
Finally, output C.Input format
The input consists of two lines, the first line is a non-negative integer a , and the second line is a non-negative integer b . Both integers do not exceed 100 digits, and the highest bit of the two numbers is not 0.
Output format
Output one line, representing the value of a + b .
Sample input
20100122201001221234567890
2010012220100122Sample output
20100122203011233454668012
Is the big number A+B
#include<bits/stdc++.h>
using namespace std;
int main()
{
int z;
int n,i,j;
char m1[110],m2[110];
int a[110],b[110],c[110];
for(i=0;i<=110;i++)//初始化int数组不要忘记
a[i]=b[i]=c[i]=0;
scanf("%s%s",m1,m2);
int l1=strlen(m1);
int l2=strlen(m2);
for(i=l1-1,j=0;i>=0;i--)//把输入的字符串倒序输入到int数组中
a[j++]=m1[i]-'0';//转换成 整形
for(i=l2-1,j=0;i>=0;i--)
b[j++]=m2[i]-'0';
for(i=0;i<=110;i++)//这里开始加法模拟;
{
c[i]+=a[i]+b[i];
if(c[i]>=10)//这里是大于等于
{
c[i]=c[i]%10;
c[i+1]++; //给前一位加上
}
}
for(z=110;c[z]==0;z--);//除去前导0,这里是==这里的冒号不要忘记了
if(z<0)
printf("0\n");
else
{
for(;z>=0;z--)//这里的输出不需要头
printf("%d",c[z]);
printf("\n");
}
return 0;
}