PAT甲级——A1010 Radix
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 <iostream>
#include <string>
#include<math.h>
#include <cctype>
#include <algorithm>
using namespace std; //最简单的一种一种格式试
int main()
{
string N1, N2;
long long int tag,radix;//防止溢出
cin >> N1 >> N2 >> tag >> radix;
if (tag == )
{
string N = N2;
N2 = N1;
N1 = N;
}
long long int aim = , n = ;//防止化为10制止时溢出
for (int i = N1.length() - , j = ; i >= ; ++j, --i)//将N1划为10进制
{
n = isdigit(N1[i]) ? (N1[i] - '') : (N1[i] - 'a' + );
aim += n * pow(radix, j);
}
long long int l, r, m;
char it = *max_element(N2.begin(), N2.end());//找出最大的元素
l = (isdigit(it) ? it - '' : it - 'a' + ) + ;//找到N2形成的最小进制
r = max(aim, l);
while (l <= r)
{
m = l + (r - l) / ;
long long int res = , n;
for (int i = N2.length() - , j = ; i >= ; ++j, --i)
{
n = isdigit(N2[i]) ? (N2[i] - '') : (N2[i] - 'a' + );
res += n * pow(m, j);
}
if (res == aim)
break;
else if (res< || res>aim)
r = m - ;
else
l = m + ;
}
if (l > r)
cout << "Impossible" << endl;
else
cout << m << endl; return ; }
PAT甲级——A1010 Radix的更多相关文章
- PAT甲级1010. Radix
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
- PAT 甲级 1010 Radix
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...
- 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 A1010.Radix 二分法
PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
随机推荐
- VS2010-MFC(MFC常用类:MFC异常处理)
转自:http://www.jizhuomi.com/software/236.html 上一节讲了CFile文件操作类,本节主要来说说MFC异常处理. 在鸡啄米C++编程入门系列的最后一节鸡啄米:C ...
- private定义的static字段子其他类能通过"类名.字段名"调用吗?
不能!
- css3@media实现原理
window.matchMedia() 基本用法 window.matchMedia方法用来检查CSS的mediaQuery语句.各种浏览器的最新版本(包括IE 10+)都支持该方法,对于不支持该方法 ...
- Django之单表查询——神奇的双下划线
1.filter中的单表查询 # 查询id>1且id<4的结果 ret = models.Person.objects.filter(id__gt=1,id__lt=4) print(re ...
- <day004>小娜显示空白+CSV文件的基本操作+普通的代理使用
小知识: 当小娜搜索显示空白的时候,怎么解决? 任务管理器结束小娜进程就好了= =*(多半是惯得,关掉就好了!) 任务1:CSV文件的基本操作 import csv import pandas as ...
- (转)Lua语言实现简单的多线程模型
转自: https://blog.csdn.net/john_crash/article/details/49489609 lua本身是不支持真正的多线程的,但是lua提供了相应的机制来实现多线程.l ...
- CVE-2016-0095提权漏洞分析
1 前言 瞻仰了k0shl和鹏哥 的漏洞分析,感慨万千,任重而道远. 2 系统环境和工具 windows 7 32旗舰版 windbg 3 poc 3.1poc复现 首先k0shl大佬给出的poc() ...
- JavaScript特效源码(7、页面特效二)
7.将站点加入频道栏 将站点加入频道栏[看详细说明] ====1.加入channel的方法:使用如下连接指向你的频道文件*.cdf. <a href="javascript:windo ...
- opencv-识别手写数字
首先拆分图片得到数据 #include "stdafx.h" #include <iostream> #include "opencv2/opencv.hpp ...
- [JZOJ4913] 【GDOI2017模拟12.3】告别
题目 描述 题目大意 给你两个排列AAA和BBB,每次随即选三个数进行轮换操作,问mmm次操作内使AAA变成BBB的概率. 思考历程 首先随便搞一下,就变成了AAA中每个数回归自己原位. 一眼望去,感 ...