A1010 Radix (25 分)
一、技术总结
- 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型)。函数内部知识,使用函数迭代器,即auto it = n.rbegin(),这里it作用就是指针一样,装的是地址,了解函数begin()是返回字符串str的一个元素地址和函数end()是返回字符串最后一个元素还要往后一位,而rbegin()和rend(),加了r后刚刚反过来了,rbegin()指向str的最后一个元素,rend()指向第一个元素还要往前一位。
- 第二个函数find_radix()用于,判断是否相等,二分查找法,里面使用了max_element()函数,但是前面加了*,因为algorithm中返回的迭代器,其实是可以加上cmp函数的,默认是从小到大排序,max_element()取最大值即最后一位min_element()取最小值。可以用于 vector 或者 vector 等,也可以用于 int arr[4] 或者 string arr[4] ,也可以用于结构体vector或者结构体数组~
二、参考代码
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cctype>
using namespace std;
long long convert(string n, long long radix){
long long sum = 0;
int index = 0, temp = 0;
for(auto it = n.rbegin(); it != n.rend(); it++){
temp = isdigit(*it) ? *it - '0' : *it - 'a' + 10;
sum += temp * pow(radix, index++);
}
return sum;
}
long long find_radix(string n, long long num){
char it = *max_element(n.begin(), n.end());
long long low = (isdigit(it) ? it - '0' : it - 'a' + 10) + 1;
long long high = max(num, low);
while(low <= high){
long long mid = (low + high) / 2;
long long t = convert(n, mid);
if(t < 0 || t > num) high = mid - 1;
else if(t == num) return mid;
else low = mid + 1;
}
return -1;
}
int main(){
string n1, n2;
long long tag = 0, radix = 0, result_radix;
cin >> n1 >> n2 >> tag >> radix;
result_radix = tag == 1 ? find_radix(n2, convert(n1, radix)) : find_radix(n1, convert(n2, radix));
if(result_radix != -1){
printf("%lld", result_radix);
}else{
printf("Impossible");
}
return 0;
}
A1010 Radix (25 分)的更多相关文章
- 1010 Radix (25 分),PTA
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 题意:给定n1.n2两个数,求可以是两 ...
- 1010 Radix (25 分)
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分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- 1010 Radix (25分)
改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...
- 【PAT甲级】1010 Radix (25 分)(二分)
题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...
- 1010 Radix (25 分)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 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 ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
随机推荐
- thinkphp5.1单模块设置
thinkphp5.1单模块 1. // 是否支持多模块'app_multi_module' => false, // 自动搜索控制器'controller_auto_search' => ...
- 如何将Azure SQL 数据库还原到本地数据库实例中
原文:https://www.jerriepelser.com/blog/restore-sql-database-localdb/ 原文作者: Jerrie Pelser 译文:如何将Azure S ...
- 使用Xmanager分析Java内存情况
今天是想看下程序在正式环境中,内存使用回收的情况,于是开始. 1.首先下载Xmanager,Xshell,然后在Xshell-设置-隧道中设置转发X11连接到Xmanager: 2.开始布置Linux ...
- Ajax和跨域请求
Ajax 一.概述 Web 程序最初的目的就是将信息(数据)放到公共的服务器,让所有网络用户都可以通过浏览器访问. 在次之前,我们可以通过以下几种方式让浏览器发出对服务端的请求,获取服务端的数据: 地 ...
- GPU跑tf-faster-rcnn demo以及训练自己的数据
https://blog.csdn.net/qq_39123369/article/details/85245512
- Bag of Tricks for Image Classification with Convolutional Neural Networks
这篇文章来自李沐大神团队,使用各种CNN tricks,将原始的resnet在imagenet上提升了四个点.记录一下,可以用到自己的网络上.如果图片显示不了,点击链接观看 baseline mode ...
- LOAD_DLL_DEBUG_EVENT 时读取 DllName
这句话是说 lpImageName 和 hFile 存在关联(associated),不是一定指向! 继续读后面那句,“这个数字可能为NULL,或者包含着被调试进程空间中的一个字符串地址.这个地址,相 ...
- 安装Goland开发工具
安装Goland开发工具 开发工具: 文本类的编辑器:记事本,notepad,sublime text,atom... 通过命令执行程序 IED:集成开发环境(integrated develop ...
- Koa 中间件的执行
Node.js 中请求的处理 讨论 Koa 中间件前,先看原生 Node.js 中是如何创建 server 和处理请求的. node_server.js const http = require(&q ...
- VS中怎样对C#项目进行单元测试
场景 SpringBoot+Junit在IDEA中实现查询数据库的单元测试: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/927 ...