1010 Radix(25 分)
1010 Radix(25 分)
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
转载来自大佬:https://blog.csdn.net/CV_Jason/article/details/80993283
代码:
#include<iostream>
#include<cctype>
#include<algorithm>
#include<cmath>
using namespace std;
long long str2num(string str,int radix){long long sum = 0;int index = 0;int per_digit = 0;for(auto t = str.rbegin();t!=str.rend();t++){per_digit = isdigit(*t)? *t - '0':*t - 'a' + 10;sum+=per_digit * pow(radix,index++);}return sum;
}
long long find_radix(string str,long long num){long long result_radix = -1;char it = *max_element(str.begin(),str.end());long long low = (isdigit(it)?it - '0':it - 'a' + 10) + 1;long long high = max(low,num);while(low<=high){long long mid = (low+high)/2;long long temp = str2num(str,mid);if(temp<0||temp>num){high = mid - 1;}else if(temp<num){low = mid + 1;}else{result_radix = mid;break;}}return result_radix;
}int main(){string N1;string N2;int tag;long long radix;while(cin>>N1>>N2>>tag>>radix){long long known_num = (tag==1?str2num(N1,radix):str2num(N2,radix));long long result = find_radix((tag==1?N2:N1),known_num);if(result!=-1)cout<<result<<endl;elsecout<<"Impossible"<<endl;}return 0;
}
我自己的写的代码不知道怎么说...... 是不对
代码:
#include <iostream>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
vector<long long>vec;
//转化成十进制的ll
ll Strtonum(string s, ll radix)
{ll sum = 0; ll radix_num = 1;string t = s;reverse(t.begin(),t.end());//反转下for(int i = 0 ; i < s.size(); i++){ll kk;if(isdigit(t[i]))kk = t[i] - '0';else kk = t[i] - 'a' + 10; sum += radix_num * kk;radix_num = radix_num * radix;} return sum;
}
ll findMaxchar(string s)
{ll maxn = 0;for(int i = 0 ; i < s.size(); i++){ll kk;if(isdigit(s[i]))kk = s[i] - '0';else kk = s[i] - 'a' + 10;maxn = max(kk, maxn);//求出最大的那个 }return maxn;
}
int main()
{string n1,n2;ll tag,radix,maxn1,maxn2;cin>>n1>>n2>>tag>>radix;//进制有个条件就是......??每个数字都不能超过....该进制if(tag == 1)maxn1 = findMaxchar(n1);else maxn1 = findMaxchar(n2);if(maxn1 >= radix){printf("Impossible\n");return 0;}ll num1 = tag == 1? Strtonum(n1,radix):Strtonum(n2,radix);ll left = 2, right = 50, mid;while(left <= right){ll mid = (left + right) / 2; //假装mid就是进制ll num2 = tag == 1? Strtonum(n2,mid):Strtonum(n1,mid);if(num2 == num1){vec.push_back(mid);right = mid - 1; }else if(num2 > num1)right = mid - 1;else if(num2 < num1)left = mid + 1;} if(tag == 1)maxn2 = findMaxchar(n2);else maxn2 = findMaxchar(n1);if(vec.size() == 0) printf("Impossible\n");else if(maxn2 >= vec[vec.size() - 1])printf("Impossible\n");else printf("%lld\n",vec[vec.size() - 1]);return 0;
}
1010 Radix(25 分)相关推荐
- 【测试点分析】1010 Radix (25 分)_37行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...
- 1010 Radix (25 分)
一.问题描述 输入两个数N1.N2,和一个标记tag.一个基数radix,其意义是:tag为1时,radix是N1的基数:tag为2时,radix是N2的基数.方便起见,以下假定tag为1,则目标就是 ...
- PAT Advanced—1010 Radix (25分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 1010 Radix (25 分)【难度: 难 / 知识点: 二分查找】
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 注意: 答案可能不是[2,36] 而是一个非 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- [Java] 1010. Radix (25)-PAT甲级
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- 【PAT - 甲级1010】Radix (25分)(二分,进制转化)
题干: Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? Th ...
- PAT A1010 Radix (25 分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT 甲级 A1010 Radix (25 分)
题目传送门 这个题用二分做,我自己写的二分呢太菜了,只能拿到19分,不放出来丢人了.后面看了yxc的代码,美妙绝伦哈. 慢慢来拜读一下. #include "bits/stdc++.h&qu ...
- PAT甲题题解-1010. Radix (25)-二分搜索
题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...
最新文章
- [JAVA EE] Thymeleaf 常用工具类
- 汽车之家机器学习平台的架构与实践
- IP地址的三种表示格式及在Socket编程中的应用
- 元数据交换绑定的秘密
- java selenium 定位frame_webdriver 定位frameset中的frame
- SpringBoot启动流程分析(四):IoC容器的初始化过程
- 付费社群聊天小程序V1.4.5+前端
- MapTask工作机制图解
- python3.7保存不了_Python3.7 traceback捕获打印和保存异常
- ArcGIS裁剪时警告 warning001003:Datum conflict between input and output
- 2021-2027全球与中国彩色TFT液晶屏市场现状及未来发展趋势
- 安装Win10 Ubuntu20.04双系统
- 算法:Climbing Stairs(爬楼梯) 6种解法
- 离散学习--笛卡尔积
- MySQL索引,检索数据库
- 寄存器和存储器的区别?
- 2022年卡塔尔世界杯,分析之前的比赛计算出谁是冠军
- oracle expdp 06512,oracle的expdp时出现ORA-39125ORA-01555ORA-06512错误导致数据库备份失败!...
- java 使用 freemarker模板 生成 word 并用 aspose 转换成PDF
- 2021.1.28课程摘要(逻辑教育-王劲胜)
热门文章
- word域高级应用 if 域 域邮件合并的值的更改 日期的更改
- 2010年11.30日 爱普生 武昌培训 Technical workshop OPOS INSTALL
- java兔子问题流程图_求龟兔赛跑的流程图 高手进来瞧瞧啊
- 生鲜电商之毒,食行生鲜模式虽好、恐也难解
- mysql复制(高可用架构方案的基础)
- Unix编程之size_t、ssize_t
- 重载类型转换操作符(overload conversion operator)
- git --amend 使用和撤销
- gluoncv 目标检测,训练自己的数据集
- mysql 出现ERROR 2002 (HY000): ....错误通用解决方法