#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void add(string a, string b)
{
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int len = a.size() + b.size() + 10;
int t = 0;
string ans(len, '0');
for (int i = 0; i < a.size(); i++)
{
ans[i] = a[i];
}
for (int i = 0; i < len; i++)
{
if (i < b.size())
{
t += (ans[i] - '0') + (b[i] - '0');
}
else
{
t += ans[i] - '0';
}
ans[i] = t % 10 + '0';
t /= 10;
}
reverse(ans.begin(), ans.end());
cout << ans.substr(ans.find_first_not_of('0')) << endl;
}
int main()
{
string a, b;
cin >> a >> b;
add(a, b);
return 0;
}
C++实现大数加法运算
最新推荐文章于 2024-09-03 12:37:25 发布