做得比较乱,修改多次后终于AC
#include <string> #include <iostream> #include <algorithm> using namespace std; bool cmp(string a,string b){ if(a == b) return false; if(a[0] == '-' && b[0] == '-') return cmp(b.substr(1),a.substr(1)); else if(a[0] == '-' && b[0] != '-') return false; else if(a[0] != '-' && b[0] == '-') return true; else{ if(a.size() > b.size() || a.size() == b.size() && a > b) return true; else return false; } } string add(string a,string b){ int c(0),i; string ret; reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); for(i = 0;i < a.size() && i < b.size();i++){ ret += (a[i] - '0' + b[i] - '0' + c) % 10 + '0'; c = (a[i] - '0' + b[i] - '0' + c) / 10; } for(;i < a.size();i++){ ret += (a[i] - '0' + c) % 10 + '0'; c = (a[i] - '0' + c) / 10; } for(;i < b.size();i++){ ret += (b[i] - '0' + c) % 10 + '0'; c = (b[i] - '0' + c) / 10; } if(c != 0) ret += c + '0'; reverse(ret.begin(),ret.end()); return ret; } string cut(string a,string b){ bool pos; if(a[0] == '-'){ pos = false; a = a.substr(1); } else{ pos = true; b = b.substr(1); } reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); int c(0),i; string ret; for(i = 0;i < a.size() && i < b.size();i++){ if(a[i] - '0' >= b[i] - '0' + c){ ret += a[i] - b[i] - c + '0'; c = 0; } else{ ret += a[i] - b[i] + 10 - c + '0'; c = 1; } } for(;i < a.size();i++){ if(a[i] - '0' >= c){ ret += a[i] - '0' - c + '0'; c = 0; } else{ ret += a[i] - '0' + 10 - c + '0'; c = 1; } } i = ret.size() - 1; while(ret[i] == '0'){ i--; } ret = ret.substr(0,i+1); if(!pos) ret += '-'; reverse(ret.begin(),ret.end()); return ret; } int main(){ int n; scanf("%d",&n); for(int i = 0;i < n;i++){ string a,b,c; cin >> a >> b >> c; if(a[0] != '-' && b[0] != '-'){ if(cmp(add(a,b),c)) printf("Case #%d: true ",i+1); else printf("Case #%d: false ",i+1); } else if(a[0] == '-' && b[0] == '-'){ if(c[0] != '-') printf("Case #%d: false ",i+1); else if(cmp(c.substr(1),add(a.substr(1),b.substr(1)))) printf("Case #%d: true ",i+1); else printf("Case #%d: false ",i+1); } else if(a[0] == '-' && b[0] != '-'){ if(cmp(a.substr(1),b)){ if(cmp(cut(a,b),c)) printf("Case #%d: true ",i+1); else printf("Case #%d: false ",i+1); } else{ if(cmp(cut(b,a),c)) printf("Case #%d: true ",i+1); else printf("Case #%d: false ",i+1); } } else{ if(cmp(a,b.substr(1))){ if(cmp(cut(a,b),c)) printf("Case #%d: true ",i+1); else printf("Case #%d: false ",i+1); } else{ if(cmp(cut(b,a),c)) printf("Case #%d: true ",i+1); else printf("Case #%d: false ",i+1); } } } return 0; }