当前位置: 首页 > news >正文

PTA甲级-1010 Radix c++

文章目录

      • Input Specification:
      • Output Specification:
      • Sample Input 1:
      • Sample Output 1:
      • Sample Input 2:
      • Sample Output 2:
  • 一、题干大意![在这里插入图片描述](https://img-blog.csdnimg.cn/68d84d3ea86e4aaab002152ae8472e05.png#pic_center)
  • 二、题解要点
  • 三、具体实现
  • 总结

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:

N1 N2 tag radix

Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set { 0-9, a-z } where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number radix is the radix of N1 if tag is 1, or of N2 if tag is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print Impossible. If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

一、题干大意在这里插入图片描述

给出两个字符串和其中一个字符串的进制,求出另一个字符串的进制,使两者的值相等。最后输出求出的进制,如果不存在这种进制就输出 “Impossible”

二、题解要点

  • 两个函数,一个用来进行进制转化,也就是把任意进制的字符串转化为十进制的数。另一个用二分法求出进制。
  • 一定要注意:在二分法求进制的过程中,求出的进制是有可能上溢的,如果不解决就只能拿15分。

三、具体实现

/**
*@Author:hhzheng
*@Date:2023/2/6  10:58
*@Filename:PAT甲级1010 Radix
*/#include<iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <algorithm>
using namespace std;/*** 输入一个字符串、一个数(表示进制) 将这个字符串转换为该十进制的数* @param n* @param radix* @return*/
long long turnDecimal(string n, long long radix) {long long theResult = 0;for (int i = n.size() - 1; i >= 0; --i) {if (n.at(i) >= '0' && n.at(i) <= '9') {theResult += (n.at(i) - '0') * pow(radix, n.size() - i - 1);}if (n.at(i) >= 'a' && n.at(i) <= 'z') {theResult += (n.at(i) - 'a' + 10) * pow(radix, n.size() - i - 1);}}return theResult;
}/*** 用二分法求出正确的进制 如果不存在返回-1* @param n* @param s* @return*/
long long getRadix(long long n, string s) {char it = *max_element(s.begin(), s.end());long long low = (isdigit(it) ? it - '0': it - 'a' + 10) + 1; //找到可能的最小的进制long long high = max(n, low); //找到最大的可能的进制,比如说 1 1 1 10 这种情况就不能high = n了while (low <= high) {long long mid = (low + high) / 2;long long temp = turnDecimal(s, mid);if (temp < 0 ||temp > n) {  //一定要有temp < 0,因为在进制转化的时候有可能会上溢high = mid - 1;} else if (temp == n) {return mid;} else {low = mid + 1;}}return -1;
}int main() {string n1, n2;long long number1, number2, radix,answerRadix;int tag;cin >> n1 >> n2 >> tag >> radix;if (tag == 1) {number1 = turnDecimal(n1, radix);answerRadix = getRadix(number1,n2);} else {number2 = turnDecimal(n2, radix);answerRadix = getRadix(number2,n1);}if (answerRadix != -1){cout << answerRadix;}else{cout <<"Impossible";}return 0;
}

总结

全网的写法思路基本差不多,决定成败的就是在如何这个进制的上溢。

http://www.lryc.cn/news/3831.html

相关文章:

  • 【项目重构】第1次思考
  • Java:SpringMVC的使用(2)
  • Elasticsearch7.8.0版本进阶——分布式集群(应对故障)
  • 【LeetCode】每日一题(2)
  • 软件设计师教程(六)计算机系统知识-操作系统知识
  • Zookeeper下载安装与集群搭建
  • Filter防火墙(8)
  • Spring事务的传播级别——包你一文通
  • C语言(C预编译指令)
  • JMeter 接口测试/并发测试/性能测试
  • 大家心心念念的RocketMQ5.x入门手册来喽
  • (考研湖科大教书匠计算机网络)第四章网络层-第三节1:IPv4地址概述
  • B站Python与OpenCV人脸识别项目超详细记录(对图片、视频、摄像头人脸的检测)
  • 【Node.js实战】一文带你开发博客项目之Koa2重构(实现session、开发路由、联调、日志)
  • 第一部分:简单句——第二章:简单句的补充
  • Spring Security简介
  • Hadoop安装 --- 简易安装Hadoop
  • 俞军产品方法论,消化吸收,要点整理
  • spring注解的开端(@Component替代bean标签的使用)
  • Matlab傅里叶谱方法求解一维波动方程
  • py3中 collections.Counter()函数典型例题
  • Linux部署达梦数据库超详细教程
  • ctfshow 每周大挑战 极限命令执行
  • 使用vue3,vite,less,flask,python从零开始学习硅谷外卖(16-40集)
  • 坚持就是胜利
  • 代码中出现转置 pose (c2w,外参矩阵) 或者转置 intrinsic (内参)矩阵的原因
  • 2023 年腾讯云服务器配置价格表出炉(2核2G/2核4G/4核8G/8核16G、16核32G)
  • 相机出图画面一半清晰,一半模糊的原因是什么?
  • Rust学习入门--【4】Rust 输出到命令行
  • Vector刷写方案—vFlash工具介绍