题目描述
As usual, there will be an A+B problem in warming up, this problem is:
Given two integers A and B, your job is to calculate the sum of A + B.
Given two integers A and B, your job is to calculate the sum of A + B.
输入
Th
ere are several test cases, For each test case:
There are two integers A, B for each case (0 ≤ A , B < 101000).
There are two integers A, B for each case (0 ≤ A , B < 101000).
输出
For each test case, output one line containing the result of A+B.
示例输入
1 2 11111111111 11111111111
示例输出
3 22222222222 大数模拟,应该不难理解#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; int main() { char a[1510],b[1510]; int c[1510]; while(scanf("%s%s",a,b)!=EOF) { memset(c,0,sizeof(c)); int i; int k1=strlen(a)-1; int k2=strlen(b)-1; int p=k1>k2?k1:k2; for(i=0; k1>=0; i++,k1--) { c[i]=a[k1]-'0'; } for(i=0; k2>=0; i++,k2--)//只有k2次循环,如果最后一次大于10,便无法进位,所以有了下面的while循环 { c[i]+=b[k2]-'0'; if(c[i]>=10) { c[i+1]++; c[i]-=10; } } while(c[i]!=0) { if(c[i]>=10) { c[i+1]++; c[i]-=10; } i++; } if(c[p+1]!=0)//两个数中大的数的最高位进位,字符串长度加1,所以如果不为0,输出的就是真正的最高位。 { printf("%d",c[p+1]); } for(p; p>=0; p--) { printf("%d",c[p]); } printf("\n"); } return 0; }