#include<iostream> #include<string> using namespace std; const int size = 50; int main() { string s1, s2; cin >> s1 >> s2; int len = (s1.length()>s2.length()) ? s1.length() : s2.length(); int a1[size] = { 0 }; int a2[size] = { 0 }; int a3[size] = { 0 }; //用数组储存两个大数 for (int i = 0; i<s1.length(); i++) a1[i] = s1[i] - ‘0‘; for (int i = 0; i<s2.length(); i++) a2[i] = s2[i] - ‘0‘; //进位储存 int carry = 0; //结果大数位数储存 int j = 0; //逆序储存结果 for (int i = len - 1; i >= 0; i--) { a3[j] = (a1[i] + a2[i] + carry) % 10; carry = (a1[i] + a2[i] + carry) / 10; j++; } if (carry != 0) { a3[j] = carry; j++; } //逆序 for (int i = 0; i < j / 2; i++) { int temp = a3[i]; a3[i] = a3[j - i - 1]; a3[j - i - 1] = temp; } //输出 for (int i = 0; i < j; i++) cout << a3[i]; cout << endl; return 0; }
时间: 2024-10-09 12:20:24