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 = ...
随机推荐
- redis集群离线安装环境搭建过程
本文是继上次redis集群重新整理的离线搭建环境,关于前期的redis集群准备工作参考我另一篇博客: http://www.cnblogs.com/qlqwjy/p/8566573.html 由于集群 ...
- IT人员必备linux安全运维之Ssh用途、安全性、身份认证以及配置……【转】
SSH一般用途 提供shell,解决telnet不安全的传输 1.修改默认ssh默认端口 vi /etc/ssh/sshd_config 修改之后重启 >systemctl restart ss ...
- 实现在点击asp:button按钮后,不刷新当前页面
方法1:return false <asp:Button ID="Button1" runat="server" Text="Button&qu ...
- C# Winform频繁刷新导致界面闪烁解决方法
C#Winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供的默认双缓冲将 ...
- 【摘要】JavaScript 的性能优化:加载和执行
1.浏览器遇到js代码会暂停页面的下载和渲染,谁晓得js代码会不会把html给强奸(改变)了: 2.延迟脚本加载:defer 属性 <html> <head> <titl ...
- python【项目】:工资管理(简易版)
功能要求: 登录系统用户认证通过后才能列出下一级菜单员工信息表 登录系统要有用户登录.注册账号.删除账号.修改密码.退出 登录密码要有加密功能 从info.txt文件读取员工及工资信息,最后通过增加, ...
- ASCII-->Ansi-->Unicode-->UTF8 关于编码 自己的总结
各种不同的编码 无非就是效率 最大化. 我猜测编码的进化流程: ASCII(American Standard Code for Information Interchange)----满足了美国和西 ...
- Codeforces Round #423 Div. 2 C-String Reconstruction(思维)
题目大意:告诉你n个字符串以及这些字符串在字符串s中出现的位置(x1,x2.....xn),要求在满足上述条件的情况下,求出字典序最小的字符串s. 解题思路:主要问题是,如果直接模拟是会超时的,比如v ...
- Effective C++学习进阶版
记得前段时间又一次拿起<Effective C++>的时候,有种豁然开朗的感觉,所以翻出了我第一遍读时做的笔记.只做参考以及查阅之用.如有需要请参阅<Effective C++> ...
- CVE-2012-1876漏洞分析
0.POC文件 <html> <body> <table style="table-layout:fixed" > <col id=&qu ...