Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
167822 B班 李文乐 进制数加法 C++ 通过 100 0 MS 252 KB 1048 2024-08-19 18:17:38

Tests(6/6):


#include <iostream> #include <string> #include <algorithm> std::string f(int base, std::string num1, std::string num2) { std::reverse(num1.begin(), num1.end()); std::reverse(num2.begin(), num2.end()); int carry = 0; std::string r; int i = 0, j = 0; while (i < num1.size() || j < num2.size() || carry) { int digit1 = (i < num1.size())? (num1[i] >= '0' && num1[i] <= '9')? num1[i] - '0' : num1[i] - 'A' + 10 : 0; int digit2 = (j < num2.size())? (num2[j] >= '0' && num2[j] <= '9')? num2[j] - '0' : num2[j] - 'A' + 10 : 0; int sum = digit1 + digit2 + carry; carry = sum / base; sum %= base; char digitChar = sum < 10? sum + '0' : sum - 10 + 'A'; r.push_back(digitChar); i++; j++; } std::reverse(r.begin(), r.end()); return r; } int main() { int base; std::string num1, num2; std::cin >> base >> num1 >> num2; std::string sum = f(base, num1, num2); std::cout << sum << std::endl; return 0; }


测评信息: