提交时间:2026-01-04 15:46:10

运行 ID: 81322

#include <iostream> #include <string> #include <algorithm> std::pair<std::string, std::string> divide(const std::string& dividend, int divisor) { if (divisor == 0) throw std::runtime_error("Division by zero"); if (dividend == "0") return {"0", "0"}; std::string quotient, remainder; std::string current; for (char digit : dividend) { current += digit; int q = 0; while (!current.empty() && std::stoll(current) >= divisor) { current = std::to_string(std::stoll(current) - divisor); ++q; } quotient += std::to_string(q); if (!current.empty()) remainder = current; } quotient.erase(0, quotient.find_first_not_of('0')); remainder.erase(0, remainder.find_first_not_of('0')); return {quotient.empty() ? "0" : quotient, remainder.empty() ? "0" : remainder}; } int main() { std::string M; int N; std::cin >> M >> N; try { auto [quotient, remainder] = divide(M, N); std::cout << quotient << "\n" << remainder << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; }