PAT 甲级 1010. Radix
来源:互联网 发布:软件著作权发表日期 编辑:程序博客网 时间:2024/06/08 08:38
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 10Sample Output 1:
2Sample Input 2:
1 ab 1 2Sample Output 2:
Impossible
题意:给定两个数字串,每个串最多10位,给定其中一个数字串的进制数,求另一个数字的进制数,不存在就输出Imposiialbe
思路:起初太年轻了,没有考虑到特殊样例,所以错误以为进制最大是36,然后错了好久,后来发现了这样的一组样例 2147483647 1 1 10
这样的话只能2分了答案了,因为需要找到最小符合的进制数,像是这样的数据 9 9 1 10,所以2分找到了答案还不行,还需要继续往前面找;
我最后的时候卡了一组小样例,因为实现姿势不好,所以会超longlong,于是就用了 unsigned long long
#include<bits/stdc++.h>using namespace std;typedef unsigned long long LL;LL minn,maxn;int main(){ string num[2]; int tag,radix; cin >> num[0] >> num[1] >> tag >> radix; int l[2]; l[0] = num[0].length(); l[1] = num[1].length(); tag--; LL ans[2] = {0}; for(int i = 0;i < l[tag];i++) { if(isdigit(num[tag][i])) ans[tag] = ans[tag] * radix + (num[tag][i] - '0'); else ans[tag] = ans[tag] * radix + (num[tag][i] - 'a' + 10); } minn = 0; for(int i = 0;i < l[!tag];i++) { if(isdigit(num[!tag][i])) minn = max(minn,LL(num[!tag][i] - '0'+1)); else minn = max(minn,LL(num[!tag][i] - 'a'+10+1)); } maxn = max(ans[tag]+1,minn); LL mid,L = minn,R = maxn; while(L < R) { //cout << L << " " << R <<endl; ans[!tag] = 0; mid = (L + R)/2; bool ok = false; for(int i = 0;i < l[!tag];i++) { if(isdigit(num[!tag][i])) ans[!tag] = ans[!tag] * mid + (num[!tag][i] - '0'); else ans[!tag] = ans[!tag] * mid + (num[!tag][i] - 'a' +10); if(ans[!tag] > ans[tag]) { break; } } //cout << L << " " <<R << " " << ans[!tag] <<endl; if(ans[!tag] < ans[tag]) L = mid + 1; else if(ans[!tag] == ans[tag]) R = mid; else R = mid - 1; } ans[!tag] = 0; for(int i = 0;i < l[!tag];i++) { if(isdigit(num[!tag][i])) ans[!tag] = ans[!tag] * L + (num[!tag][i] - '0'); else ans[!tag] = ans[!tag] * L + (num[!tag][i] - 'a' +10); } //cout << L << " " <<ans[!tag] <<endl; if(ans[!tag] != ans[tag]) printf("Impossible\n"); else printf("%d\n",L); return 0;}
- PAT 甲级 1010. Radix
- PAT 甲级 1010. Radix
- [PAT-甲级]1010.Radix
- 【PAT甲级】1010. Radix (25)
- 1010. Radix (25) PAT 甲级
- PAT甲级1010. Radix (25)
- pat甲级1010. Radix (25)
- PAT甲级 1010. Radix (25)
- PAT 甲级 1010.Radix (25)
- PAT 甲级 1010. Radix (25)
- PAT甲级练习1010. Radix (25)
- PAT甲级1010:Radix
- 1010. Radix (25)-PAT甲级真题(二分法)
- PAT甲级 1010.Radix(25) 题目翻译与答案
- [PAT甲级]1010. Radix (25)(求另一个数的基数)
- PAT-甲级-1010 Radix(25)
- 1010. Radix (25)-PAT
- 【PAT】1010. Radix (25)
- hdfs常用命令和java操作hdfs
- 面试题35—相关题目1
- .NET的ConcurrentDictionary,线程安全集合类
- 主成分分析(PCA降维)与最小二乘-原理以及详细推导
- View事件分发机制
- PAT 甲级 1010. Radix
- JPA一对一、一对多、多对多
- vlc+dss实现视频实时传输及远程显示
- 文章标题
- 面试题35—相关题目
- HashMap源码分析及面试题解答
- 秒懂,Java 注解 (Annotation)你可以这样学
- iOS的三种多线程技术 对比分析
- Android 换肤