PAT甲级1010. Radix (25)

题意:

给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是“是”,如果6是十进制数,110是二进制数。

现在对于任何一对正整数N1和N2,你的任务是找到一个数字的基数,而另一个数字的基数。

输入规格:

每个输入文件包含一个测试用例。每个案例占用一个包含4个正整数的行:

N1 N2标签基数

这里N1和N2每个不超过10位数。数字小于其基数,并从集合{0-9,a-z}中选择,其中0-9表示十进制数0-9,a-z表示十进制数10-35。

如果“标签”为1,最后一个数字“radix”为N1的基数,如果“tag”为2,则为N2。

输出规格:

对于每个测试用例,以一行打印另一个数字的基数,使得方程式N1 = N2为真。如果方程不可能,打印“不可能”。如果解决方案不是唯一的,输出最小可能的基数。

思路:

就是给你两个数,已知其中一个数的进制,然后求另外一个数是多少进制就可以让两个数相等。

暴力遍历会在测试点7超时。 二分搜索后,如果不考虑溢出会在测试点10报错。

二分搜索查找进制,下界是n2中最大的一个数字 + 1;上界是n1的10进制数 + 1;别的没有什么坑点感觉。

ac代码:

C++

// pat1010_radix.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<unordered_map>
#include<cmath> using namespace std; int main()
{
string n1, n2;
int tag, radix;
cin >> n1 >> n2 >> tag >> radix;
long long a = 0, b = 0,res;
if (tag == 2) swap(n1, n2); char ch;
int index = 0;
while (!n1.empty())
{
ch = n1.back();
if (ch >= 'a' && ch <= 'z')
{
a += (ch - 'a' + 10) * pow(radix, index);
}
else
{
a += (ch - '0') * pow(radix, index);
}
n1.pop_back();
index++;
} long long temp = 0;
for (int i = 0; i < n2.length(); i++)
{
if (n2[i] > temp) temp = n2[i];
}
if (temp >= 97) temp -= 87;
else temp -= 48; long long left = temp + 1;
long long right = a + 1;
res = a + 2;
while(left <= right)
{
temp = (left + right) / 2;
index = 0;
b = 0;
string tempn2 = n2;
while (!tempn2.empty())
{
ch = tempn2.back();
if (ch >= 'a' && ch <= 'z')
{
b += (ch - 'a' + 10) * pow(temp, index);
}
else
{
b += (ch - '0') * pow(temp, index);
}
tempn2.pop_back();
index++;
if (b > a || b < 0) break;
}
if (a == b)
{
res = min(res, temp);
right--;
}
else if (b > a || b < 0)
{
right = temp - 1;
}
else if (b < a)
{
left = temp + 1;
}
}
if (res == a + 2) cout << "Impossible" << endl;
else cout << res << endl;
return 0;
}

PAT甲级1010. Radix的更多相关文章

  1. 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 ...

  2. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  3. PAT 甲级 1010 Radix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...

  4. PAT甲组 1010 Radix (二分)

    1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...

  5. PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新

    题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...

  6. PAT甲级——A1010 Radix

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  7. PAT Advanced 1010 Radix(25) [⼆分法]

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  8. PAT 解题报告 1010. Radix (25)

    1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...

  9. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

随机推荐

  1. linux下生成core dump文件方法及设置【转】

    转自:http://blog.csdn.net/mrjy1475726263/article/details/44116289 源自:http://andyniu.iteye.com/blog/196 ...

  2. ELK&ElasticSearch5.1基础概念及配置文件详解【转】

    1. 配置文件 elasticsearch/elasticsearch.yml 主配置文件 elasticsearch/jvm.options jvm参数配置文件 elasticsearch/log4 ...

  3. 剑指offer算法题

    数组中只出现一次的数字(一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字): 解法在于位运算中的异或,直接异或可以得到这两个数的异或,按照最后的有效数字位可以 ...

  4. 做php网站后台开发,在Linux系统上进行更好吗?【转载】

    1. PHP是开源软件,它在bsd/linux/win下都有很好的正式版及孪生版.并非开发php就必须要在linux下进行.主机服务商们习惯性的把asp与php分为两个主机系列几进行销售.由于asp只 ...

  5. 数学中的Sin和Cos是什么意思?(转)

    数学中的Sin和Cos是什么意思? 作者:admin 分类:生活随笔 发表于 2012年03月21日 16:48 问:数学中的Sin和Cos是什么意思? 答:sin, cos, tan 都是三角函数, ...

  6. NGUI优化之Drawcall

    今天在运行之前的程序时,无意中发现一个简单的menu菜单页面drawcall居然达到接近30了,这个数值感觉太高了. 后网上查询关于降低drawcall的方法,发现主要有以下几点: 1.少用Panel ...

  7. js + -操作符

    js + 举例说明最有效了... "11"+1='111' "11"+'1'="111" 11+1=12 大概的感觉就是+操作符会优先输入S ...

  8. android Webview Html5 相关文章

    Android WebView的使用集锦(支持Html5) http://blog.csdn.net/l_215851356/article/details/69239643 WebView详解与简单 ...

  9. MySQL增删改数据

    1.增加数据 ,); /*插入所有字段.一定依次按顺序插入--字符串与日期需要加单引号,数字不需要,各个字段之间用逗号分隔*//*注意不能少或者多字段值*/ ,) /*按字段名插入数据,中间用逗号隔开 ...

  10. chrome浏览器插件开发经验(一)

    最近在进行chrome浏览器插件的开发,一些小的经验总结随笔. 1.首先,推荐360的chrome插件开发文档:http://open.chrome.360.cn/extension_dev/over ...