PAT 甲级 1010 Radix
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536
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
代码:
#include <bits/stdc++.h>
using namespace std; string N1, N2;
int radix, tag;
long long sum = 0; long long Pow(long long a, long long b) {
long long ans1 = 1; while(b) {
if(b % 2) {
ans1 = ans1 * a;
b --;
} else {
a = a * a;
b /= 2;
}
}
return ans1;
} long long num(string s, int system) {
int ls = s.length();
reverse(s.begin(), s.end());
long long ans = 0;
if(system <= 10) {
for(int i = 0; i < ls; i ++)
ans += (s[i] - '0') * Pow(system, i);
} else {
int temp;
for(int i = 0; i < ls; i ++) {
if(s[i] >= '0' && s[i] <= '9')
temp = s[i] - '0';
else temp = s[i] - 'a' + 10; ans += temp * Pow(system, i);
}
}
return ans;
} long long Find(string s, long long res) {
char it = *max_element(s.begin(), s.end());
long long l = (isdigit(it) ? it - '0': it - 'a' + 10) + 1;
long long r = max(res, l);
long long mid; while(l <= r) {
mid = (l + r) / 2;
long long rec = num(s, mid);
if(rec == res) return mid;
else if(rec > res || rec < 0) r = mid - 1;
else l = mid + 1;
}
return -1;
} int main() {
cin >> N1 >> N2 >> tag >> radix;
int l1 = N1.length(), l2 = N2.length();
long long out = 0;
if(tag == 1) {
sum = num(N1, radix);
out = Find(N2, sum);
} else {
sum = num(N2, radix);
out = Find(N1, sum);
} if(out == -1) printf("Impossible\n");
else printf("%lld\n", out);
return 0;
}
明天就过年啦 希望新年会很多不一样
FHFHFH
PAT 甲级 1010 Radix的更多相关文章
- PAT甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- PAT甲组 1010 Radix (二分)
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
- PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新
题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...
- PAT甲级——A1010 Radix
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
随机推荐
- 20155212 C语言实现linux下pwd命令的两种方法
20155212 C语言实现linux下pwd命令的两种方法 学习pwd命令 通过man pwd命令查看 pwd [OPTION],一般不加参数 -P显示当前目录的物理路径 -L显示当前目录的连接路径 ...
- 2017-2018-1 20155230 mypwd实现
mypwd实现 每个文件夹都有以".",".."为名的文件.首先记录"."所拥有的i-节点号,然后使用chdir向上进入父目录查找含有刚记录 ...
- python+soket实现UDP协议的客户/服务端中文聊天程序
没什么特别的东西,网上烂大街的C/S框架.(基于windows 7 + python 3.4) 为了实现中文聊天,我加入了一点修改: msg.encode('utf-8') # msg 为输入(且将要 ...
- flask的继承和包含
为了方便使用重复的页面,我们也可以使用继承模板.还有包含模板,一般使用包含,俩个都不是很好理解,我只是用完的理解简单介绍一下,他们的用法打不相同,却又有类似之处 我们访问页面的时候在最上边会有导航的信 ...
- Git使用教程(一)---本地git管理
之前写过不少小demo,因为没有很好的整理,很多demo都找不到了,必要的时候又要重新写这些demo:这是一个很头疼的问题,很烦躁.网上看到git的版本控制以及托管,很nice.开始学习使用git,关 ...
- halcon二 图像校正
1.get_image_size(Image : : : Width, Height) 返回图像的尺寸. 2.parameters_image_to_world_plane_centered (Cam ...
- [算法总结] 20 道题搞定 BAT 面试——二叉树
本文首发于我的个人博客:尾尾部落 0. 几个概念 完全二叉树:若二叉树的高度是h,除第h层之外,其他(1~h-1)层的节点数都达到了最大个数,并且第h层的节点都连续的集中在最左边.想到点什么没?实际上 ...
- 微信小程序-帝国cms会员系统调用
在用户->管理会员字段,增加如下字段:openidsession_keylsktokennicknameheadimg设置用户名长度然后,在系统,系统变最设置,用户设置,将注册用户名设置长度改成 ...
- python编辑修改haproxy配置文件--文件基础操作
一.需求分析 有查询,删除,添加的功能 查询功能:查询则打印查询内容,如果不存在也要打印相应的信息 删除功能:查询到要删除内容则删除,打印信息. 添加功能:同上. 二.流程图 三.代码实现 本程序主要 ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...