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 ...
随机推荐
- Noip2015Day2T3 运输计划
题目链接 problem 一棵n个点带边权的树,有m个条路径.选择一条边,将其权值变为0,使得长度最长的路径长度最小.求该长度最小为多少. solution 其实仔细一想并不难. 删除一条边会导致所有 ...
- SVO稀疏图像对齐代码分析
SVO使用稀疏直接法计算两帧之间的初始相机位姿,即使用两帧之间稀疏的4*4 patch的光度误差为损失函数,使用G-N优化算法获得两帧之间的位姿变换,由于没有特征匹配过程效率较高.相比自己实现的稀疏直 ...
- HTML文件通过jQuery引入其他HTML文件报错has been blocked by CORS policy
HTML通过jQuery引入模板 完整报错 新创建一个chrome快捷方式,命名为chrome-debug 右键属性,在目标后添加参数,原始路径如下 "C:\Program Files (x ...
- subprocess模块(了解)
目录 一.subprocess模块 一.subprocess模块 subprocess模块允许你去创建一个新的进程让其执行另外的程序,并与它进行通信,获取标准的输入.标准输出.标准错误以及返回码等.更 ...
- oracle--共享磁盘挂载
01,查看挂载的磁盘 [root@SHLPDBWX01 ~]# fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders Units ...
- 大话设计模式Python实现-职责链模式
职责链模式(Chain Of Responsibility):使多个对象都有机会处理请求,从而避免发送者和接收者的耦合关系.将对象连成链并沿着这条链传递请求直到被处理 下面是一个设计模式的demo: ...
- Thymeleaf入门与基础语法
1.简介 Thymeleaf是用来开发Web和独立环境项目的现代服务器端Java模板引擎. Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板 - HTML.可以在直接浏览器中正确显示 ...
- CSS-页面超出手机屏幕
手机页面左滑,页面超出手机屏幕. 解决方法: html,body{ overflow-x: hidden; } 从而解决问题,锁住横向滑动的屏幕.
- 2019 年 GitHub 上最热门的 Java 开源项目
1.JavaGuide https://github.com/Snailclimb/JavaGuide Star 22668 [Java 学习 + 面试指南] 一份涵盖大部分 Java 程序员所需要掌 ...
- Redisson实现分布式锁(2)—RedissonLock
Redisson实现分布式锁(2)-RedissonLock 有关Redisson实现分布式锁上一篇博客讲了分布式的锁原理:Redisson实现分布式锁---原理 这篇主要讲RedissonLock和 ...