pat甲级 1010 Radix (二分查找)

做题+看代码2h...debug半小时

#include <iostream>
#include <cctype>
#include <algorithm>
#include <cmath>
#include<string>
using namespace std;
long long convert(string n, long long radix) {
	long long _n = 0;
	int temp=0, index = 0;
	for (auto c = n.rbegin(); c != n.rend(); c++) {
		temp = isdigit(*c) ? *c - '0' : *c - 'a' + 10;
		_n +=temp* pow( radix,index++);
	}
	return _n;
}
long long find_radix(string n,long long num) {
	char it = *max_element(n.begin(), n.end());
	long long low = (isdigit(it) ? it - '0': it - 'a' + 10) + 1;
	long long high = max(num, low);
	while (low <= high) {
		long long mid = (low + high) / 2;
		long long t = convert(n, mid);
		if (t<0||t > num)	high = mid - 1;
		else if (t == num)	return mid;
		else  low = mid + 1;
	}
	return -1;
}
int main() {
	string n1, n2;
	long long tag=0, radix=0, result_radix=0;
	cin >> n1 >> n2 >> tag >> radix;
	if (tag == 1)
		result_radix = find_radix(n2, convert(n1, radix));
	else
		result_radix = find_radix(n1, convert(n2, radix));
	if (result_radix != -1)
		cout << result_radix << endl;
	else
		cout << "Impossible\n";
	return 0;
	system("pause");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值