一、技术总结

  1. 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型)。函数内部知识,使用函数迭代器,即auto it = n.rbegin(),这里it作用就是指针一样,装的是地址,了解函数begin()是返回字符串str的一个元素地址和函数end()是返回字符串最后一个元素还要往后一位,而rbegin()和rend(),加了r后刚刚反过来了,rbegin()指向str的最后一个元素,rend()指向第一个元素还要往前一位。
  2. 第二个函数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 分)的更多相关文章

  1. 1010 Radix (25 分),PTA

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 题意:给定n1.n2两个数,求可以是两 ...

  2. 1010 Radix (25 分)

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

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

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

  4. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

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

  5. 1010 Radix (25分)

    改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...

  6. 【PAT甲级】1010 Radix (25 分)(二分)

    题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...

  7. 1010 Radix (25 分)

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

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

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

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

随机推荐

  1. x86-64数据格式、通用寄存器与操作数格式

    x86-64数据格式.通用寄存器与操作数格式 数据格式 ​ Intel用术语"字(word)"表示16位数据类型,32位为"双字(double words)", ...

  2. vscode源码分析【一】从源码运行vscode

    安装git,nodejs和yarn 安装Python27,3.x版本的不行,确保它在你的环境变量里: 安装gulp npm install --global gulp-cli 安装windows bu ...

  3. iOS: 本地通知的前后变化(iOS10)

    一.介绍  通知和推送是应用程序中很重要的组成部分.本地通知可以为应用程序注册一些定时任务,例如闹钟.定时提醒等.远程推送则更强大,提供了一种通过服务端主动推送消息到客户端的方式,服务端可以更加灵活地 ...

  4. python画一片绿叶给你

    怎么用 turtle 画一个 π 字,于是我顺手到网上大致搜了下,发现网上没有画这个 π 字的,接着又用谷歌加英文搜索了下,还是没找到现成的答案. 不过通过这次搜索意外发现了一个有趣的网站,网站上有大 ...

  5. git 添加add readme.txt 报fatal: pathspec 'readme.txt' did not match any files错误

    刚刚接触git版本管理器,跟着廖雪峰老师的git教程学习,在创建一个新的文件时,使用的是$ git add readme.txt指令,但是报出fatal: pathspec 'readme.txt' ...

  6. 1+x 证书 Web 前端开发 css 专项练习

    官方QQ群 1+x 证书 Web 前端开发 css 专项练习 http://blog.zh66.club/index.php/archives/192/

  7. solidity 智能合约之间的调用

    智能合约之间的调用 在区块链上,有些功能往往无法通过一个智能合约完成,此时便会用到智能合约之间的调用.本篇文章带大家通过具体示例来了解一下智能合约之间的调用. 在智能合约的编译过程中,有两种情况:调用 ...

  8. python基础(10):文件操作

    1. 初识文件操作 使⽤python来读写⽂件是非常简单的操作.我们使⽤open()函数来打开⼀个⽂件,获取到⽂ 件句柄,然后通过⽂件句柄就可以进⾏各种各样的操作了,根据打开⽅式的不同能够执⾏的操 作 ...

  9. Vue-nodeJS环境搭建

    Node.jsNode.js是一个基于Chrome V8引擎的[JavaScript运行环境]. Node.js使用了一个事件驱动.非阻塞式I/O 的模型.Node.js是一个让JavaScript运 ...

  10. Fiddler使用技巧:强大的数据文本编解码功能

    - 总览 打开Fiddler后,使用快捷键Ctrl+e或者点击菜单Tools-->TextWizard...即可进入TextWizard界面. 界面上方为输入框,用来输入需要编码或解码的数据. ...