Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
78436 sh25_wangtaojie 高精度减法 C++ 编译错误 0 MS 0 KB 1034 2025-12-26 15:46:04

Tests(0/0):


#include <iostream> #include <string> #include <algorithm> std::string subtract(const std::string& a, const std::string& b) { if (a == b) return "0"; bool negative = false; if (a.size() < b.size() || (a.size() == b.size() && a < b)) { std::swap(a, b); negative = true; } std::string result; int borrow = 0; for (int i = a.size() - 1, j = b.size() - 1; i >= 0; --i) { int digitA = a[i] - '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 += std::to_string(diff); if (j >= 0) --j; } std::reverse(result.begin(), result.end()); result.erase(0, result.find_first_not_of('0')); return negative ? "-" + result : result; } int main() { std::string a, b; std::cin >> a >> b; std::cout << subtract(a, b) << std::endl; return 0; }


测评信息: