PAT1010(二分查找求二进制)
来源:互联网 发布:excel数据链接更新 编辑:程序博客网 时间:2024/06/05 22:30
- Radix (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
此题的坑点挺多:
1>如果单纯的暴力,没有给明确的进制要求,所以一定会爆int,因为是十位,所以不会爆long long,题目的描述是:给定一个确定进制的数,问是否存在某一个进制使得的数字等于给定的数字;题目没有给定最大进制数,采用long long合适。
2>错误做法:从最小进制下界从二开始暴力求,不是,题意中给出最小进制下界是所有位最大的数加一
3>就是下界确定也不一定能过,测试点7会超时,这样就可以想到二分来进行找适合的二进制数,上界最大也只可能是已知的那个数 + 1;
4>步骤:
用long long
找最小下界,最大上界,二分查找匹配进制,输出
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<cmath>using namespace std;map<char,int>mp;long long num1,num2;//害怕函数库的函数进制大时结果会不准确long long Pow(int n,long long m){ if(m == 0){ return 1; } else{ long long x = 1; for(long long i = 1;i <= m;++i) { x *= n; } return x; }}//将字符串按进制转换成十进制long long CharTolonglong(char *s,long long radix){ long long p = 0; for(int i = strlen(s) - 1;i >= 0;--i) { p += mp[s[i]] * Pow(radix,strlen(s) - 1 - i); if(Pow(radix,strlen(s) - 1 - i) < 0){ return -1; } //计算进制的过程中会溢出,溢出返回-1 if(p < 0){ return -1; } } return p;}int main(){ char s1[12]; char s2[13]; for(int i = 0;i <= 9;++i) { mp['0' + i] = i; } for(int i = 10;i <= 35;++i) { mp['a' + i - 10] = i; } scanf("%s %s",s1,s2); long long tag,radix; cin >> tag >> radix; char s[13]; long long left = 0,right = 0; if(tag == 1){ //找二分的时候的最小上界和最大上界 for(int i = strlen(s2) - 1;i >= 0;--i) { if(left < mp[s2[i]]){ left = mp[s2[i]]; } } left++; num1 = CharTolonglong(s1,radix); strcpy(s,s2); right = num1 + 1; } if(tag == 2){ for(int i = strlen(s1) - 1;i >= 0;--i) { if(left < mp[s1[i]]){ left = mp[s1[i]]; } } left++; num1 = CharTolonglong(s2,radix); strcpy(s,s1); right = num1 + 1; } //cout << left << " " << right << endl; long long ans = 0; while(left <= right) { long long mid = (left + right) / 2; num2 = CharTolonglong(s,mid); if(num2 < 0 || num2 > num1){ right = mid - 1; } else{ if(num2 < num1){ left = mid + 1; } else{ ans = mid; break; } } } if(!ans){ cout << "Impossible" << endl; } else{ cout << ans << endl; } return 0;}
- PAT1010(二分查找求二进制)
- pat1010-穷举vs二分查找
- 二分查找求下界
- 二分查找求平方根
- 二分查找、三分查找求极点、二分求等比数列【模板】
- 二分查找求上界 下界
- 二分查找求上、下界
- 二分查找求上、下界
- 二分查找之求中值
- 求最大公约数,二分查找法
- 二分查找求上下界
- pat1010
- PAT1010
- PAT1010
- pat1010
- pat1010
- PAT1010
- PAT1010
- 数据库设计三大范式
- Linux系统优化
- java 题目:判断101-200之间有多少个素数,并输出所有素数。
- Java普通代码块,构造代码块,静态代码块,成员变量的执行顺序的代码实例
- iOS 分享多张图片到第三方平台
- PAT1010(二分查找求二进制)
- centos6 zabbix_agentd客户端安装部署
- 神经网络与深度学习第二周-1- Python Basics With Numpy
- 前端面试题6
- LeetCode.690 Employee Importance
- C#中的IP和IPEndPoint
- Android自定义控件字体大小设置。
- Crashing Robots
- 前端面试题7