提交时间:2024-08-19 21:56:06

运行 ID: 168030

#include <iostream> #include <string> using namespace std; bool isValidInBase(const string& num, int base) { for (char c : num) { int digit; if (c >= '0' && c <= '9') { digit = c - '0'; } else { digit = c - 'A' + 10; } if (digit >= base) { return false; } } return true; } long long baseToDecimal(const string& num, int base) { long long decimalValue = 0; long long power = 1; for (int i = num.size() - 1; i >= 0; --i) { char digit = num[i]; int digitValue; if (digit >= '0' && digit <= '9') { digitValue = digit - '0'; } else { digitValue = digit - 'A' + 10; } decimalValue += digitValue * power; power *= base; } return decimalValue; } int main() { string p, q, r; cin >> p >> q >> r; int minBase = 17; for (int base = 2; base <= 16; ++base) { if (isValidInBase(p, base) && isValidInBase(q, base) && isValidInBase(r, base)) { long long decimalP = baseToDecimal(p, base); long long decimalQ = baseToDecimal(q, base); if (decimalP * decimalQ == decimalR) { minBase = min(minBase, base); } } } if (minBase == 17) { cout << 0 << endl; } else { cout << minBase << endl; } return 0; }