算法
(进制转换)
法 $1$:直接把 $A$ 和 $B$ 当字符串处理,即 $res = res * k + (c - ‘0’)$,其中 $c$ 为字符串中的数字。
法 $2$:可以利用 std::string
的 stoll
函数实现进制转换。
C++ 代码
// 法1:
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using ll = long long;
int read(int k) {
string s;
cin >> s;
int res = 0;
for (char c : s) {
res = res * k + (c - '0');
}
return res;
}
int main() {
int k;
cin >> k;
int a = read(k);
int b = read(k);
cout << (ll)a * b << '\n';
return 0;
}
// 法2:
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::string;
using ll = long long;
int main() {
int k;
string a, b;
cin >> k >> a >> b;
ll p1 = stoll(a, 0, k), p2 = stoll(b, 0, k);
ll ans = p1 * p2;
cout << ans << '\n';
return 0;
}