#include using namespace std; int main() { string a, b, c, d, ans; int x, y, z = 0; cin >> a >> b >> c >> d; int maxSize = max(max(a.size(), b.size()), max(c.size(), d.size())); ans.resize(maxSize + 1, '0'); for (int i = 1; i <= maxSize; i++) { if (i > a.size()) x = 0; else x = a[a.size() - i] - '0'; if (i > c.size()) y = 0; else y = c[c.size() - i] - '0'; ans[maxSize + 1 - i] = (x + y + z) % 10 + '0'; z = (x + y + z) / 10; } if (z > 0) { ans[0] = z + '0'; } cout << ans << endl; return 0; }

C++ 字符串加法实现及代码优化

原文地址: https://www.cveoy.top/t/topic/pMJt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录