题目大意:
http://codevs.cn/problem/3115/
题解:
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int ans[501] = {0}; int sub(string a, string b)// a is the big number, b is smaller. { int m = a.length()-1; int n = b.length()-1; int k = 0; for(int i = m; i >= 0; i--) { if(n >= 0) { if(a[i] >= b[n]) ans[k++] = int(a[i]-b[n]); else { ans[k++] = int(a[i]+10-b[n]); int tmp = i-1; while(a[tmp] == '0') { a[tmp] = '9'; tmp--; } a[tmp] = char(a[tmp]-1); } n--; } else { ans[k++] = int(a[i] - '0'); } } return k; } int main() { string a,b; cin >> a >> b; int k; if(a.length() < b.length() || (a.length() == b.length() && a < b)) { k = sub(b,a)-1; cout << "-"; } else if(a.length() > b.length() || (a.length() == b.length() && a > b)) { k = sub(a,b)-1; } else { cout << "0"; } while(ans[k] == 0) k--; for(int i = k; i >= 0; i--) { cout << ans[i]; } return 0; }