1010. Radix (25)
2015-11-22 00:47
309 查看
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
[/code]
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
--------------------------------华丽的分割线-----------------------
分析:这题有点恶心,一开始我认为Radix最大是36,结果好几个点死活通不过,后来发现Radix有可能很大,所以就加大了范围。
代码:
#include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #define Maxn 11 char input[2][Maxn]; int lengthofinput[2]; long long int minradix[2]; int MinPossibleRadix(char input[]); long long int ConvertToDecimal(char input[],int length,long long int radix); int main(void) { int Tag; long long int Radix; scanf("%s %s %d %lld",input[0],input[1],&Tag,&Radix); lengthofinput[0] = strlen(input[0]); lengthofinput[1] = strlen(input[1]); minradix[0] = MinPossibleRadix(input[0]); minradix[1] = MinPossibleRadix(input[1]); long long int DefiniteSum = ConvertToDecimal(input[Tag-1],lengthofinput[Tag-1],Radix); long long int leftradix = minradix[2-Tag]; long long int rightradix = DefiniteSum + 1; long long int midradix = 0; long long int thissum = 0; bool equal = false; while(leftradix <= rightradix) { midradix = (leftradix + rightradix) / 2; thissum = ConvertToDecimal(input[2-Tag],lengthofinput[2-Tag],midradix); if(thissum > DefiniteSum || thissum == -1) { rightradix = midradix - 1; } else if(thissum < DefiniteSum) { leftradix = midradix + 1; } else { equal = true; break; } } if(equal) printf("%lld",midradix); else printf("Impossible"); system("pause"); return 0; } int MinPossibleRadix(char input[]) { int length = strlen(input); int minradix = 0; for(int i=0;i<length;++i) { if(isdigit(input[i])) { input[i] = input[i] - '0'; } if(islower(input[i])) { input[i] = input[i] - 'a' +10; } if(input[i] > minradix) minradix = input[i]; } return minradix+1; } long long int ConvertToDecimal(char input[],int length,long long int radix) { long long int sum = 0; for(int i=0;i<length;++i) { sum = sum*radix + input[i]; if(sum < 0) return -1; } return sum; }
[/code]
相关文章推荐
- Android自定义ScrollView实现一键置顶功能
- 【转】python version 2.7 required,which was not found in the registry
- 黑马程序员-IO流
- VirtualBox 5.0(虚拟机软件)里,安装Fedora遇到的问题!!
- git+新浪云 实践 : 网站应用搭建
- 七大算法汇总
- VirtualBox 5.0(虚拟机软件)里,安装Fedora遇到的问题!!
- HDU ACM 2093 考试排名
- HDU 1248 寒冰王座(母函数)
- TreeSet comparator字符串长度排序
- apache+php+mysql常见集成环境安装包
- Android的材料设计兼容库(Design Support Library)
- 文章标题
- Python的函数参数传递:传值?引用?
- DEDECMS调用顶级栏目+顶级栏目ID的方法
- 几种序列化协议(protobuf,xstream,jackjson,jdk,hessian)相关数据对比
- APIX_身份证图像识别技术(附代码)
- 机器学习相关——SVD分解
- 黑马程序员--java笔记12--IO流
- 10大基础实用算法及其讲解