pat 1010. Radix (25)

https://www.patest.cn/contests/pat-a-practise/1010


代碼來自http://blog.csdn.net/jtjy568805874/article/details/50782565


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

typedef unsigned long long ull;
string a, b;
int tag, radix, res;
ull ans, l, r;
ull get(char c) {
	if ('0' <= c && c <= '9') return c - '0';
	return c - 'a' + 10;
}

int main() {
	cin >> a >> b >> tag >> radix;
	if (tag == 2) swap(a, b);
	for (int i = 0; a[i]; i++) ans = ans*radix + get(a[i]);
	for (int i = 0; b[i]; i++) l = max(l, get(b[i]));
	for (l++, r = ans + 1; l <= r;) {
		ull mid = l + r >> 1;
		ull check = 0;
		for (int i = 0; b[i]; i++) check = check*mid + get(b[i]);
		if (check == ans) res = mid;
		if (check >= ans) r = mid - 1; else l = mid + 1;
	}
	res ? printf("%d\n", res) : printf("Impossible\n");
	return 0;
}


發佈了66 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章