P1601 A+B Problem(高精)
题目描述
高精度加法,相当于 a+b problem,不用考虑负数。
输入格式
分两行输入。\(a,b \leq 10^{500}\)。
输出格式
输出只有一行,代表 \(a+b\) 的值。
输入输出样例 #1
输入 #1
1
1
输出 #1
2
输入输出样例 #2
输入 #2
1001
9099
输出 #2
10100
说明/提示
\(20\%\) 的测试数据,\(0\le a,b \le10^9\);
\(40\%\) 的测试数据,\(0\le a,b \le10^{18}\)。
AC代码
#include<bits/stdc++.h>
using namespace std;string gadd(string a,string b){string c=" ";int len=max(a.size(),b.size());while(a.size()<len) a='0'+a;while(b.size()<len) b='0'+b;int jw=0;for(int i=len-1;i>=0;i--){int x=a[i]-'0',y=b[i]-'0';int z=x+y+jw;jw=z/10;z=z%10;char ch='0'+z;c=ch+c;}if(jw!=0) return '1'+c;return c;
}int main(){string a,b;cin>>a>>b;cout<<gadd(a,b);return 0;
}