本题用例无须考虑负数,若有负数仍可以用这些基运算实现
#include <iostream> #include "cmath" using namespace std; struct bigInteger { int nums[1000]; int length; bigInteger(); bigInteger(string str); bool operator<( bigInteger B); bigInteger operator+(bigInteger B); bigInteger operator-(bigInteger B); bigInteger operator*(bigInteger B); void print(); }; bigInteger::bigInteger() { for (int i = 0; i < 1000; i++) { nums[i] = 0; } length = 0; } bigInteger::bigInteger(string str) { for (int i = 0; i < 1000; i++) { nums[i] = 0; } length = str.size(); for (int i = 0; i < length; i++) { nums[i] = str[length - 1 - i] - '0'; } } bool bigInteger::operator<(bigInteger B) { if (length < B.length) return true; else if (length > B.length) return false; else { for (int i = length - 1; i >= 0; i--) { if (nums[i] < B.nums[i]) return true; else if (nums[i] > B.nums[i]) return false; } return false;//全部数字相等 } } bigInteger bigInteger::operator+(bigInteger B) { bigInteger ans; int carry = 0; int bound = max(length, B.length); for (int i = 0; i < bound ; i++) { int temp = nums[i] + B.nums[i] + carry; ans.nums[i] = temp % 10; ans.length++; carry = temp / 10; } if (carry != 0) ans.nums[ans.length++] = 1; return ans; } bigInteger bigInteger::operator-(bigInteger B) { bigInteger ans; int carry = 0; for (int i = 0; i < length; i++) { int temp = nums[i] - carry - B.nums[i]; if (temp < 0) { temp += 10; carry = 1; } else { carry = 0; } ans.nums[ans.length++] = temp; } while (ans.nums[ans.length - 1] == 0 && ans.length >= 1) { ans.length--; } return ans; } bigInteger bigInteger::operator*(bigInteger B) { bigInteger ans; ans.length=length+B.length; for (int i = 0; i < B.length; i++) { for (int j = 0; j < length; j++) { ans.nums[i + j] += B.nums[i] * nums[j]; } } int carry = 0; for (int i = 0; i < length + B.length; i++) { carry = ans.nums[i] / 10; ans.nums[i+1]+=carry; ans.nums[i] %= 10; } while (ans.nums[ans.length - 1] == 0 && ans.length >= 1) { ans.length--; } return ans; } void bigInteger::print() { for (int i = length - 1; i >= 0; i--) { cout << nums[i]; } cout << endl; } int main() { string a, b; while (cin >> a >> b) { // 注意 while 处理多个 case // cout << bigIa + b << endl; (bigInteger(a)+bigInteger(b)).print(); bigInteger A=bigInteger(a); bigInteger B=bigInteger(b); if(B<A) (bigInteger(a)-bigInteger(b)).print(); else{cout<<'-'; (B-A).print(); } (bigInteger(a)*bigInteger(b)).print(); } } // 64 位输出请用 printf("%lld")