/* * * * 只考虑了 正整数相加 的情况 * */ #include<iostream> #include<string> #include<cstring> using namespace std; int main() { string str1, str2; cin >> str1 >> str2; int a1[1000], a2[1000]; memset(a1, 0, 1000 * sizeof(int)); memset(a2, 0, 1000 * sizeof(int)); int len1, len2, lenmax; len1 = str1.length(); len2 = str2.length(); if (len1>len2) lenmax = len1; else lenmax = len2; int i; for (i = 0; i<len1; i++) a1[i] = str1[len1 - i - 1] - '0'; for (i = 0; i != len2; i++) a2[i] = str2[len2 - i - 1] - '0'; int k = 0; int b[1000]; for (i = 0; i != lenmax; i++) { b[i] = (a1[i] + a2[i] + k) % 10; k = (a1[i] + a2[i] + k) / 10; } if (k == 1) cout << '1'; while (lenmax--) cout << b[lenmax]; cout << endl; return 0; }
20 total views, 1 views today