PAT (Advanced Level) Practise 1010. Radix (25) C++
2017-03-19 21:54
447 查看
This passage is dedicated to 46 times of Answer Error.
https://www.patest.cn/contests/pat-a-practise/1010
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.
Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.
Input Specification:
Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.
Output Specification:
For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.
Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible
https://www.patest.cn/contests/pat-a-practise/1010
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.
Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.
Input Specification:
Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.
Output Specification:
For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.
Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible
#include<iostream> #include<cstring> #include<climits> using namespace std; long long cal(char &ch) { return ch>='0'&&ch<='9'?ch-'0':ch-'a'+10; } long long str2x(char* val, long long radix) { long long result = 0; for (int i=0;i<strlen(val);i++) { result *= radix; result += cal(val[i]); if (LLONG_MAX/result<radix && result != 0) return LLONG_MAX; } return result; } int main() { #ifdef ONLINE_JUDGE #else freopen("D:\\in.txt", "r", stdin); #endif char a[10],b[10]; long long radixA,radixB; int tag; long long left_radix{},right_radix; long long valueA,valueB; cin>>a>>b>>tag>>radixA; if (tag==2) { char tmp[10]; strcpy(tmp,a); strcpy(a,b); strcpy(b,tmp); } valueA = str2x(a,radixA); for (int i=0;i<strlen(b);i++) { left_radix=left_radix>cal(b[i])+1?left_radix:cal(b[i])+1; } right_radix = valueA + 1; while(left_radix<=right_radix) { radixB = (left_radix + right_radix)/2; valueB = str2x(b,radixB); if (valueB > valueA) right_radix = radixB - 1; else if (valueB < valueA) left_radix = radixB + 1; else if (valueB = valueA) { cout<<radixB; return 0; } } cout<<"Impossible"; return 0; }
相关文章推荐
- PAT (Advanced Level) Practise 1010 Radix (25)
- PAT (Advanced Level) Practise 1010. Radix (25)
- 1010. Radix (25)——PAT (Advanced Level) Practise
- 【c++】PAT (Advanced Level)1010. Radix (25)
- PAT (Advanced Level) Practise 1010. Radix (25)
- 1010. Radix (25)——PAT (Advanced Level) Practise
- 【PAT Advanced Level】1010. Radix (25)
- PAT (Advanced Level) Practise 1010 Radix
- PAT (Advanced Level)1010. Radix (25) 二分搜索
- 【PAT】【Advanced Level】1010. Radix (25)
- PAT (Advanced Level) Practise 1001-1010
- 【C++】PAT(advanced level)1036. Boys vs Girls (25)
- 【C++】PAT(advanced level)1044. Shopping in Mars (25)
- 【C++】PAT (advanced level)1033. To Fill or Not to Fill (25)*
- 【C++】PAT(advanced level)1048. Find Coins (25)
- 【C++】PAT(advanced level)1056. Mice and Rice (25)
- 1017. Queueing at Bank (25) @ PAT (Advanced Level) Practise
- 【C++】PAT(advanced level)1047. Student List for Course (25)
- 浙大PAT (Advanced Level) Practise 1003. Emergency (25)
- 【C++】PAT(advanced level)1037. Magic Coupon (25)