1.使用string读取a和b,如果符号相同,按照字符串来处理,因为两个负数或者两个正数相加,会超过最大的取值范围
2.如果符号不同,那么a+b一定会在范围内,可以使用long long进行处理和比较
//#include<string> //#include<stack> //#include<unordered_set> //#include <sstream> //#include "func.h" //#include <list> #include <iomanip> #include<unordered_map> #include<set> #include<queue> #include<map> #include<vector> #include <algorithm> #include<stdio.h> #include<iostream> #include<string> #include<memory.h> #include<limits.h> #include<stack> using namespace std; /* 1000 9223372036854775807 -9223372036854775808 0 9223372036854775807 -9223372036854775806 0 9223372036854775808 -9223372036854775807 1 9223372036854775808 -9223372036854775807 2 1 1 0 1 1 2 1 1 3 -1 -1 0 -1 -1 -2 -1 -1 -3 */ long long str2long(string a) { long long ans = 0; for (int i = 0; i < a.size(); i++) ans = ans * 10 + a[i] - '0'; return ans; } string reverseStr(string a) { for (int i = 0; i < a.size() / 2; i++) swap(a[i], a[a.size() - 1 - i]); return a; } int main(void) { int n; cin >> n; for (int k = 0; k < n;k++) { bool isBigger = true; string a, b; cin >> a >> b; bool aPositive = false; bool bPositive = false; //如果是负数,则进行标记和去掉负号 if (a[0] == '-') { a = a.substr(1); aPositive = true; } if (b[0] == '-') { b = b.substr(1); bPositive = true; } if ((aPositive&&bPositive) || (!aPositive&&!bPositive)) { string ans = ""; if (a.size() > b.size()) swap(a, b);//保证a最短 a = reverseStr(a); b = reverseStr(b); int i = 0; int carry = 0; for (; i < a.size(); i++) { int sum = a[i] - '0' + b[i] - '0' + carry; carry = sum / 10; char c = sum % 10 + '0'; ans = c + ans; } for (; i < b.size(); i++) { int sum = b[i] - '0' + carry; carry = sum / 10; char c = sum % 10 + '0'; ans = c + ans; } if (carry != 0) { char c = carry + '0'; ans = c + ans; } //输入c string c; cin >> c; bool cPositive = false; if (c[0] == '-') { c = c.substr(1); cPositive = true; } if (aPositive&&bPositive && cPositive) {//a+b为负数,c为负数 if (ans.size() > c.size())//a+b的绝对值大于c的绝对值 isBigger = false; else if (ans.size() == c.size()) { for (int i = 0; i < ans.size(); i++) { if (ans[i]>c[i]) {//a+b的绝对值大于c的绝对值 isBigger = false; break; } else if (ans[i] < c[i]) {//a+b的绝对值小于c的绝对值 isBigger = true; break; } } if (ans == c) isBigger = false;//相等的话,为false } else isBigger = true; } else if (aPositive&&bPositive && !cPositive) {//a+b为负数,c为正数 isBigger = false; } else if (!aPositive&&!bPositive && cPositive) {//a+b为正数,c为负数 isBigger = true; } else {//a+b为正数,c为正数 if (ans.size() > c.size())//a+b的绝对值大于c的绝对值 isBigger = true; else if (ans.size() == c.size()) { for (int i = 0; i < ans.size(); i++) { if (ans[i]>c[i]) {//a+b的绝对值大于c的绝对值 isBigger = true; break; } else if (ans[i] < c[i]) {//a+b的绝对值小于c的绝对值 isBigger = false; break; } } if (ans == c) isBigger = false;//相等的话,为false } else isBigger = false; } } else { long long aInt = str2long(a), bInt = str2long(b); long long ans; if (!aPositive&&bPositive) ans = aInt - bInt; else ans = bInt - aInt; long long c; cin >> c; if (ans > c) isBigger = true; else isBigger = false; } if (isBigger) printf("Case #%d: true\n", k + 1); else printf("Case #%d: false\n", k + 1); } return 0; }