← Real problems

sac22cc3jp4

easyimplementationbase-conversionStatement on DMOJ

CP Lang

4 lineschecks
// sac22cc3jp4: convert each number from one base to another.
for _ in 0..read<1..=1_000>():
    let (from, s, to) = read<(2..=36, Str, 2..=36)>()
    print(s.parse_int(base: from)!.to_str(base: to))

Accepted C++

61 lines · 1.40 s on the judge
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...) 1412
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

void solve() {
	int q;
	cin >> q;
	while (q--) {
		int b1, b2;
		string s;
		cin >> b1 >> s >> b2;
		int cur = stoi(s, nullptr, b1);
		string ans;
		while (cur) {
			ans += to_string(cur % b2);
			cur /= b2;
		}
		reverse(begin(ans), end(ans));
		cout << ans << endl;
	}
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cin.exceptions(ios::failbit);
	int t = 1;
	// cin >> t;
	for (int _ = 1; _ <= t; _++) {
		dbg(_);
		// cout << "Case #" << _ << ": ";
		solve();
	}
}