PAT甲级1010. Radix
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的更多相关文章
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...
- 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 = ...
随机推荐
- Django1.10中文文档—模型
模型是你的数据的唯一的.权威的信息源.它包含你所储存数据的必要字段和操作行为.通常,每个模型都对应着数据库中的唯一一张表. 基础认识: 每个model都是一个继承django.db.models. ...
- docker之构建redis-cluster集群
下载和编译redis安装包 参考:https://www.cnblogs.com/cwp-bg/p/8094914.html # 从官方网站下载安装包,注意,当前在哪个目录下执行命令,下载的包将在哪个 ...
- flask基础之jijia2模板使用基础(二)
前言 在以前前后端不分离的时代,后台程序员往往又当爹又当妈,需要将前端程序员写的h5页面填充模板语言.而jijia2是一门十分强大的python的模板语言,是flask框架的核心模块之一.先简单介绍一 ...
- 牛B的日本精神
在汤森路透评选出的<2015全球创新企业百强>榜单里,日本以40家高居榜首,力压美国的35家.而中国内地无一入围. 在中国媒体上,我们见到的日本是“失去的20年”,经济衰退.创新能力丧 ...
- 增加Android模拟器空间(Internal Storage)
转载 http://vase.iteye.com/blog/2114664 初学Android,发现模拟器上有不少限制,譬如标题中的存储限制,无论用ADT Manager如何设置,内部存储空间不会 ...
- redis常见数据操作
redis中有5种常见的数据类型,针对这5种数据类型有着相应的数据操作. 1.String(键值对为String - String) set k1 v1 get k1 getset k1 v1 - h ...
- Unity 软件使用事项
打开旧版工程 目前发现两种方式来触发升级程序: 1.Unity软件启动时选择旧版工程,触发更新 2.直接打开旧版工程的场景文件,触发更新 在使用中发现一种错误做法,不知道是不是共性问题,在此先记录 ...
- django的orm中F对象的使用
今天不巧就用上了. 就是将数据库的字段,自增1的场景. from django.db.models import F DeployPool.objects.filter(name=deployvers ...
- VMware虚拟机VMware Authorization Service不能启动问题
出现VMware Authorization Service不能启动问题,注意要在安装VMware Player时使用管理员权限
- checkbox 更改样式
html: <div class="wrap"> <p>1. 勾选</p> <input type="checkbox" ...