PAT Radix[二分][进制转换][难]
1010 Radix (25)(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 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible
//感觉这道题就整个很难读懂。
//N1 N2 tag radix
//如果tag是1的话,那么radix就是N1的基数;如果tag是2的话,那么radix就是N2的基数。radix的取值范围是0-35.也就是最多是35进制。
//我的想法是先将tag所指的数转成10进制,然后找到另一个数digit最大的,如果当前2进制已经>另一个,那么就impossible;否则就增大;就是很普通的想法。不会想到使用二分法,就是看到题解二分法之后也没想到二分法是如何使用的。
代码来自:https://www.liuchuo.net/archives/2458
- #include <iostream>
- #include <cctype>//isdigit函数,从来没见过这个函数,厉害了。
- #include <algorithm>
- #include <cmath>
- #include<string>
- #include<stdio.h>
- using namespace std;
- long long convert(string n, long long radix) {
- //
- long long sum = ;
- int index = , temp = ;
- // for (auto it = n.rbegin(); it != n.rend(); it++) {//isdigit需要配合这样的for循环使用。
- // temp = isdigit(*it) ? *it - '0' : *it - 'a' + 10;
- // sum += temp * pow(radix, index++);
- // }
- int len=n.size();//也可以这样将radix进制转化为10进制。
- for(int i=;i<len;i++){
- if(n[i]>=''&&n[i]<='')temp=n[i]-'';
- else temp=n[i]-'a'+;
- sum=sum*radix+temp;
- }
- return sum;
- }
- long long find_radix(string n, long long num) {//num是10进制的数
- char it = *max_element(n.begin(), n.end());//找出最大的元素。
- //进制数最小是最大元素+1
- //如n=s9jik;则low=29,
- //若n="10",那么进制就会取到num.!!
- long long low = (isdigit(it) ? it - '': it - 'a' + ) + ;
- long long high = max(num, low);
- while (low <= high) {
- long long mid = (low + high) / ;
- long long t = convert(n, mid);
- if (t < || t > num) high = mid - ;
- else if (t == num) return mid;
- else low = mid + ;
- }
- return -;
- }
- int main() {
- string n1, n2;
- long long tag = , radix = , result_radix;
- cin >> n1 >> n2 >> tag >> radix;
- result_radix = tag == ? find_radix(n2, convert(n1, radix)) : find_radix(n1, convert(n2, radix));
- if (result_radix != -) {
- printf("%lld", result_radix);
- } else {
- printf("Impossible");
- }
- return ;
- }
1.使用二分法,low很好确定,high是另一个数的10进制数;(没想到进制可以这么大)很厉害。
2.radix制如何转换成10进制。
PAT Radix[二分][进制转换][难]的更多相关文章
- PAT甲级 进制转换题_C++题解
进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...
- JS 进制转换
十进制转换成其他进制 objectname.toString([radix]) objectname 必选项.要得到字符串表示的对象. radix 可选项.指定将数字值转换为字符串时的进制. 例如 ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
- 【String与基本类型之间的转换】以及【进制转换】
1. 基本数据类型---->字符串类型: 方法一:使用连接一个空字符串,例如 基本数据类型+“” : 方法二:静态方法 String.valueOf(),具体有: String.valueOf ...
- java 13-4 Integer和String、int之间的转换,进制转换
1.int类型和String类型的相互转换 A.int -- String 推荐用: public static String valueOf(int i) 返回 int 参数的字符串表示形式. B. ...
- Swift3.0 进制转换
Swift3.0 进制转换 模块可以直接使用,写的不是很好,欢迎来喷 // Data -> HexStrings func dataToHexStringArrayWithData(data: ...
- java中Integer包装类的具体解说(java二进制操作,全部进制转换)
程序猿都非常懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往须要操作Integer,或者各种进制的转换等等.我今天就为大家具体解说一下Integer的使用吧.看代码: ...
- C++中进制转换问题
一直在刷题的时候,都会遇到一个坑,就是进制转换的问题.而每一次都傻乎乎的自己去实现一个.所以算是对以前的坑的一个总结. itoa 函数 itoa是广泛应用的非标准C语言和C++语言扩展函数.由于它不是 ...
- JavaSE教程-03Java中分支语句与四种进制转换
一.分支语句 计算机源于生活,程序模拟现实生活,从而服务生活 行为模式 1,起床,刷牙,洗脸,吃早餐,上课,回家,睡觉(顺序性) 2,如果时间不太够,打个滴滴快车,如果时间够,坐个地铁(选择性) 3, ...
随机推荐
- 【node.js】Error: CERT_UNTRUSTED
背景 : 在linux centos7 上 进行npm 命令是报错: Error: CERT_UNTRUSTED 解决办法: 关掉HTTPS就好了 npm config set strict-ssl ...
- TCP数据传输过程详解
在学习三次握手的时候,我们知道其中有seq.ack两个序列号. 如果不仔细了解,那么可能只知道发回去的时候要加一. 下文将着重介绍,关于序列号的传输过程. 最关键的一句话:序列号为当前端成功发送的数据 ...
- 【19道XSS题目】不服来战!(转)
[19道XSS题目]不服来战! 记得第一次接触xss这个概念是在高中,那个时候和一个好基友通过黑客X档案和黑客手册.第一次接触到了除了游戏以外的电脑知识,然后知道了,原来电脑除了玩游戏还可以搞这些,从 ...
- css3整理--word-wrap/word-break/white-space
word-wrap语法: word-wrap : normal | break-word normal : 默认值,单词如果单词超长,会冲出边界(单个单词超长,在当前行显示) break-word : ...
- nginx(二)----ubuntu14.04下启动或重启和关闭nginx
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 一.启动 /usr/local/nginx/sbin/nginx或者cd /usr/ ...
- 剑指offer题目记录
1.如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStri ...
- Egret中的三种单例写法
1 普通的单例写法 as3中也是这么个写法. 缺点:每个单例类里都要写instance和getInstance. class Single{ private static instance:Singl ...
- 几个解决k染色问题的指数级做法
几个解决k染色问题的指数级做法 ——以及CF908H题解 给你一张n个点的普通无向图,让你给每个点染上k种颜色中的一种,要求对于每条边,两个端点的颜色不能相同,问你是否存在一种可行方案,或是让你输出一 ...
- iOS - UIScreen的 bound、frame、scale属性
A UIScreen object contains the bounding rectangle of the device’s entire screen. When setting up you ...
- 高盛为什么认为中国AI领域将超越美国?
不久前,高盛发布的名为<中国在人工智能领域崛起>的研究报告,报告中,高盛认为中国已经成为AI领域的主要竞争者,中国政府建设“智慧型经济”和“智慧社会”的目标将有可能推动中国未来GDP的增长 ...