pat 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 N?1?? and N?2??, 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

思路:将给定的数转换成十进制数,然后对另一个数字用循环逐个找到对应进制的十进制数的大小   本来用stoi函数的,写的很方便,但是通过牛客给的测试数据发现,stoi的进制范围是有要求的,2-36进制的准换难点:(1)进制范围的确定,下界 l 等于待测的数字的各个位上的最大的数+1,上届是给定数字的十进制形式+1         (2) 要用long long 以及对于代码中溢出的处理   ,,,,long long 溢出后小于零疑问:题中要求如果答案不唯一,那么输出最小的,但是感觉二分来写会满足不了题意,但是最后ac了

代码如下:
 1 #include <cstdio>
 2 #include <string>
 3 #include <iostream>
 4 #include <algorithm>
 5 #define LL long long
 6 using namespace std;
 7
 8  LL ch_num(string s,int r){
 9     LL res=0;
10     int len=s.length();
11     for(int i=0;i<len;i++){
12         if(s[i]>=‘0‘&&s[i]<=‘9‘)
13             res=res*r+(s[i]-‘0‘);
14         else
15             res=res*r+(s[i]-‘W‘);
16     }
17     if(res<0)
18         return -1;//溢出处理
19     return res;
20 }
21 LL maxcnt(string s){
22     LL maxn=0,t;
23     int len=s.length();
24     for(int i=0;i<len;i++){
25         if(s[i]>=‘0‘&&s[i]<=‘9‘){
26             t=s[i]-‘0‘;
27             maxn=max(maxn,t);
28         }
29         else{
30             t=s[i]-‘W‘;
31             maxn=max(maxn,t);
32         }
33     }
34     return maxn+1;
35 }
36
37 int main(void)
38 {
39     int tag,rad;
40     string fin,sen;
41     cin>>fin>>sen>>tag>>rad;
42
43     LL num,l;
44     string str;
45     if(tag==1){
46         num=ch_num(fin,rad);
47         l=maxcnt(sen);
48         str=sen;
49     }
50     else{
51         num=ch_num(sen,rad);
52         l=maxcnt(fin);
53         str=fin;
54     }
55
56     LL mid,tem;
57     LL r=num+1;
58     while(l<=r){
59         mid=(l+r)>>1;
60         tem=ch_num(str,mid);
61         if(tem==num){
62             printf("%lld\n",mid);
63             return 0;
64         }
65         if(tem>num || tem==-1)
66             r=mid-1;
67         else
68             l=mid+1;
69     }
70     printf("Impossible\n");
71
72     return 0;
73 }
 

原文地址:https://www.cnblogs.com/gn1314/p/12141626.html

时间: 2024-08-27 13:06:17

pat Radix(25分)的相关文章

PTA 10-排序5 PAT Judge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Spe

1025 PAT Ranking (25 分)

1025 PAT Ranking (25 分) Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately

PAT Advanced 1093 Count PAT&#39;s (25分)

The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters. Now given any string, you are supposed to tell the numb

PTA乙级 (*1095 解码PAT准考证 (25分))

1095 解码PAT准考证 (25分) https://pintia.cn/problem-sets/994805260223102976/problems/1071786104348536832 题目大意:给出一组学生的准考证号和成绩,准考证号包含了等级(乙甲顶),考场号,日期,和个人编号信息,并有三种查询方式查询一:给出考试等级,找出该等级的考生,按照成绩降序,准考证升序排序查询二:给出考场号,统计该考场的考生数量和总得分查询三:给出考试日期,查询改日期下所有考场的考试人数,按照人数降序,考

1025 PAT Ranking (25分)

1025 PAT Ranking (25分) 1. 题目 2. 思路 设置结构体, 先对每一个local排序,再整合后排序 3. 注意点 整体排序时注意如果分数相同的情况下还要按照编号排序 4. 代码 #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; struct stu{ int location_number; char

PATA1075 PAT Judge (25 分)

The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Specification: Each input file contains one test case. For each case, the first line

1040 有几个PAT(25 分)

字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位(T). 现给定字符串,问一共可以形成多少个 PAT? 输入格式: 输入只有一行,包含一个字符串,长度不超过10?5??,只包含 P.A.T 三种字母. 输出格式: 在一行中输出给定字符串中包含多少个 PAT.由于结果可能比较大,只输出对 1000000007 取余数的结果. 输入样例: APPAPT 输出样例

PAT Basic 1095 解码PAT准考证 (25 分)

PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月.日顺次各占 2 位: 最后 11~13 位是考生编号,范围从 000 到 999. 现给定一系列考生的准考证号和他们的成绩,请你按照要求输出各种统计信息. 输入格式: 输入首先在一行中给出两个正整数 N(≤)和 M(≤),分别为考生人数和统计要求的个数. 接下来 N 行,每行给出一个考生的准考证号和

1153 Decode Registration Card of PAT (25 分)

A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic; the 2nd - 4th digits are the test site number, ranged from 101 to 999; the 5th - 10th digits