提交时间:2025-12-26 15:37:15
运行 ID: 78308
#include <iostream> #include <string> #include <algorithm> std::pair<std::string, std::string> divide(const std::string& dividend, const std::string& 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() && current >= divisor) { current = std::to_string(std::stoll(current) - std::stoll(divisor)); ++q; } quotient += std::to_string(q); if (!current.empty()) remainder = current; } // Remove leading zeros 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, N; std::cin >> M >> N; try { auto [quotient, remainder] = divide(M, N); std::cout << quotient << " " << remainder << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; }