Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
81247 sh25_zhangyy 大整数减法 C++ 通过 0 MS 252 KB 989 2026-01-04 15:43:09

Tests(4/4):


#include <iostream> #include <vector> #include <algorithm> using namespace std; string subtractLargeIntegers(const string& a, const string& b) { vector<int> result; int borrow = 0; int i = a.size() - 1, j = b.size() - 1; while (i >= 0 || j >= 0) { int digitA = (i >= 0) ? a[i--] - '0' : 0; int digitB = (j >= 0) ? b[j--] - '0' : 0; int diff = digitA - digitB - borrow; if (diff < 0) { diff += 10; borrow = 1; } else { borrow = 0; } result.push_back(diff); } while (result.size() > 1 && result.back() == 0) { result.pop_back(); } string finalResult; for (auto it = result.rbegin(); it != result.rend(); ++it) { finalResult += to_string(*it); } return finalResult; } int main() { string a, b; cin >> a >> b; string result = subtractLargeIntegers(a, b); cout << result; return 0; }


测评信息: