给定两个正整数,计算它们的差。
#include <iostream> #include <vector> using namespace std; vector<int> a, b, c; bool cmp () { if (a.size() != b.size()) return a.size() > b.size(); for (int i = a.size() - 1; i >= 0; i--) { if (a[i] == b[i]) continue; return a[i] > b[i]; } return true; } void sub (vector<int>& a, vector<int>& b) { for (int i = 0, t = 0; i < a.size(); i++) { t = a[i] - t; if (i < b.size()) t -= b[i]; if (t >= 0) { c.push_back(t); t = 0; } else { t += 10; c.push_back(t); t = 1; } } while (c.size() > 1 && c.back() == 0) c.pop_back(); } int main() { string x, y; cin >> x >> y; for (int i = x.size() - 1; i >= 0; i--) a.push_back(x[i] - '0'); for (int i = y.size() - 1; i >= 0; i--) b.push_back(y[i] - '0'); if (cmp()) sub(a, b); else { cout << "-"; sub(b, a); } for (int i = c.size() - 1; i >= 0; i--) cout << c[i]; return 0; }
标签:return,高精度,int,back,--,push,减法,size From: https://www.cnblogs.com/leetothemoon/p/16939689.html