PAT 甲级 1010 Radix

https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536

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

代码:

#include <bits/stdc++.h>
using namespace std;

string N1, N2;
int radix, tag;
long long sum = 0;

long long Pow(long long a, long long b) {
    long long ans1 = 1;

    while(b) {
        if(b % 2) {
            ans1 = ans1 * a;
            b --;
        } else {
            a = a * a;
            b /= 2;
        }
    }
    return ans1;
}

long long num(string s, int system) {
    int ls = s.length();
    reverse(s.begin(), s.end());
    long long ans = 0;
    if(system <= 10) {
        for(int i = 0; i < ls; i ++)
            ans += (s[i] - ‘0‘) * Pow(system, i);
    } else {
        int temp;
        for(int i = 0; i < ls; i ++) {
            if(s[i] >= ‘0‘ && s[i] <= ‘9‘)
                temp = s[i] - ‘0‘;
            else temp = s[i] - ‘a‘ + 10;

            ans += temp * Pow(system, i);
        }
    }
    return ans;
}

long long Find(string s, long long res) {
    char it = *max_element(s.begin(), s.end());
    long long l = (isdigit(it) ? it - ‘0‘: it - ‘a‘ + 10) + 1;
    long long r = max(res, l);
    long long mid;

    while(l <= r) {
        mid = (l + r) / 2;
        long long rec = num(s, mid);
        if(rec == res) return mid;
        else if(rec > res || rec < 0) r = mid - 1;
        else l = mid + 1;
    }
    return -1;
}

int main() {
    cin >> N1 >> N2 >> tag >> radix;
    int l1 = N1.length(), l2 = N2.length();
    long long out = 0;
    if(tag == 1) {
        sum = num(N1, radix);
        out = Find(N2, sum);
    } else {
        sum = num(N2, radix);
        out = Find(N1, sum);
    } 

    if(out == -1) printf("Impossible\n");
    else printf("%lld\n", out);
    return 0;
}

  明天就过年啦 希望新年会很多不一样 

FHFHFH

原文地址:https://www.cnblogs.com/zlrrrr/p/10350969.html

时间: 2024-08-30 08:15:01

PAT 甲级 1010 Radix的相关文章

PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新

题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化为10进制之后比较大小(下面统称已知进制数为N1,未知进制数为N2) 2.虽然两个数都只有10位,且每一位上的数字是从‘0’~‘z’,分别代表0~35,但是这并不意味值这题的进制范围就是2~36,radix完全有可能很大很大到long long,写‘0’~‘z’只是为了让结果计算出来相对小一些,并且

PAT 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 i

1010. Radix (25)——PAT (Advanced Level) Practise

题目信息: 1010. Radix (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 b

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

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 N?1?? and N?2??, your task is

1010 Radix

1010 Radix 注意点 如111 1 1 10类似情况下,若n为个位数,如果本身比另一个数小,则多少的进制都是没有用的(可能会造成空循环而超时),不过好像没有这么一个测试用例 进制应该比最少数据中的最大的数要大一,如8最少是9进制的数,z最少是36进制的数 注意算法中的超时问题如9999999999 11 1 10很容易超时,注意匹配的算法 如果用c等强类型语言写,注意溢出问题,int类型肯定是不够的 python3代码 def getNum(num, radix): sum = 0 co

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m