当前位置: 代码迷 >> 综合 >> 【PAT甲级】1010 Radix
  详细解决方案

【PAT甲级】1010 Radix

热度:85   发布时间:2024-03-07 14:55:31.0

题目链接:1010 Radix 

#include <iostream>
#include <algorithm>
using namespace std;long long toRadix10(string s, long long radix){//任意进制转10进制数long long a = 1, b = 0;for(int i = (int)s.length() - 1; i >= 0; i--){if(s[i] >= 'a' && s[i] <= 'z') s[i] += '0' - 'a' + 10;b += a * (s[i] - '0');a *= radix;}return b;
}int main(){string N[2];long long M[2], tag, radix, low, hign;cin >> N[0] >> N[1] >> tag >> radix;tag--;M[tag] = toRadix10(N[tag], radix);char temp = *max_element(N[1-tag].begin(), N[1-tag].end());//找到最大值low = (isdigit(temp) ? temp - '0' : temp - 'a' + 10) + 1;//进制最小比最大值大1hign = max(low, M[tag]);//进制最大不超过另一个数while(low <= hign){//二分法查找否则测试点7过不去long long mid = (low + hign) / 2;M[1-tag] = toRadix10(N[1-tag], mid);if(M[1-tag] < 0 || M[1-tag] > M[tag]) hign = mid - 1;//溢出或比较大else if(M[1-tag] == M[tag]){cout << mid;return 0;}else low = mid + 1;}cout << "Impossible";
}