http://pat.zju.edu.cn/contests/pat-practise/1010
来源:互联网 发布:java.io jar包 编辑:程序博客网 时间:2024/05/01 00:09
1010. Radix (25)
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
- #include<iostream>
- #include<cstdio>
- #include<memory.h>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #include<cmath>
- #include<cstdlib>
- #include<string>
- using namespace std;
- char s1[15];
- char s2[15];
- long long getvalue(char* s, int radix){
- long long value = 0;
- int len = strlen(s);
- int num;
- for(int i=0;i<len;++i){
- if(s[i]>='0' && s[i]<='9')
- num = s[i]-'0';
- else
- num = s[i]-'a'+10;
- value = value*radix + num;
- }
- return value;
- }
- int findlowradix(char * s){
- int len = strlen(s);
- int ans = 2;
- for(int i=0;i<len;++i){
- if(s[i]>='0' && s[i]<='9')
- ans = s[i] - '0' + 1;
- else
- ans = s[i] - 'a' + 11;
- }
- return ans;
- }
- int main(){
- freopen("in.txt", "r", stdin);
- int tag, radix;
- scanf("%s %s %d %d", s1, s2, &tag, &radix);
- char * a = s1;
- char * b = s2;
- if(tag==2){
- char * c = a;
- a = b;
- b = c;
- }
- long long v = getvalue(a, radix);
- bool flag = false;
- int r = findlowradix(b);
- for(;r<=3000;++r){
- long long res = getvalue(b, r);
- if(res==v){
- printf("%d\n", r);
- flag = true;
- break;
- }
- }
- if(!flag)
- printf("Impossible\n");
- fclose(stdin);
- return 0;
- }
- http://pat.zju.edu.cn/contests/pat-practise/1010
- http://pat.zju.edu.cn/contests/pat-practise/1030
- http://pat.zju.edu.cn/contests/pat-practise/1034
- http://pat.zju.edu.cn/contests/pat-practise/1016
- http://pat.zju.edu.cn/contests/pat-practise/1032
- http://pat.zju.edu.cn/contests/pat-practise/1037
- http://pat.zju.edu.cn/contests/pat-practise/1021
- http://pat.zju.edu.cn/contests/pat-practise/1040
- http://pat.zju.edu.cn/contests/pat-practise/1038
- http://pat.zju.edu.cn/contests/pat-practise/1025
- http://pat.zju.edu.cn/contests/pat-practise/1023
- http://pat.zju.edu.cn/contests/pat-practise/1029
- http://pat.zju.edu.cn/contests/pat-practise/1035
- http://pat.zju.edu.cn/contests/pat-practise/1024
- http://pat.zju.edu.cn/contests/pat-practise/1031
- http://pat.zju.edu.cn/contests/pat-practise/1028
- http://pat.zju.edu.cn/contests/pat-practise/1020
- http://pat.zju.edu.cn/contests/pat-practise/1015
- http://pat.zju.edu.cn/contests/pat-practise/1012
- Qt版本跟Qt SDK版本对照表
- 【集合动规】混乱的队伍
- android 发送 SMS 详解
- 夏五月,崔杼弑君
- http://pat.zju.edu.cn/contests/pat-practise/1010
- 美国副国务卿本月16日至17日将访华-亚太局势-中日关系-钓鱼岛问题
- 【Java】程序员应该掌握的10项技能
- http://pat.zju.edu.cn/contests/pat-practise/1009
- http://pat.zju.edu.cn/contests/pat-practise/1007
- http://pat.zju.edu.cn/contests/pat-practise/1005
- 介绍Servlet容器
- Emacs org进阶
- http://pat.zju.edu.cn/contests/pat-practise/1004