PAT 甲级 A1010 (2019/02/20)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL Map[256];            // 0 ~ 9, a ~ z 与 0 ~ 35 的对应
LL Y = 1;               // 定义LL型的Y
LL inf = (1LL << 63) - Y;   // long long 的最大值 2^63 - 1,注意括号 

void init(){
    for(char c = '0'; c <= '9'; c++){
        Map[c] = c - '0';   // 将 '0' ~ '9' 映射到 0 ~ 9
    }
    for(char c = 'a'; c <= 'z'; c++){
        Map[c] = c - 'a' + 10;  // 将 'a' ~ 'z' 映射到 10 ~ 35
    }
}
// 将 a 转化为十进制,t为上界( LL inf = (1LL << 63) - 1;   )
LL convertNum10(char a[], LL radix, LL t){
    LL ans = 0;
    int len = strlen(a);
    for(int i = 0; i < len; i++){
        ans = ans * radix + Map[a[i]];      // 进制转换
        if(ans < 0 || ans > t) return -1;   // 溢出或超过N1的十进制
    }
    return ans;
}
// N2 的十进制与 t 比较
int cmp(char N2[], LL radix, LL t){
    int len = strlen(N2);
    LL num = convertNum10(N2, radix, t);    // 将N2转化为十进制
    if(num < 0) return 1;   // 溢出,肯定是 N2 > t
    if(t > num) return -1;  // t较大,返回 -1
    else if(t == num) return 0; // 相等,返回 0
    else return 1;          // num较大,返回 1
}
// 二分求解 N2 的进制
LL binarySearch(char N2[], LL left, LL right, LL t){
    LL mid;
    while(left <= right) {
        mid = (left + right) / 2;
        int flag = cmp(N2, mid, t);     // 判断 N2 转化为十进制后与 t 比较
        if(flag == 0) return mid;       // 找到解,返回 mid
        else if(flag == -1) left = mid + 1; // 往右子区间继续找
        else right = mid - 1;               // 往左子区间继续找
    }
    return -1;  // 解不存在
}
// 求最大的位数
int findLargestDigit(char N2[]){
    int ans = -1, len = strlen(N2);
    for(int i = 0; i < len; i++) {
        if(Map[N2[i]] > ans) {
            ans = Map[N2[i]];
        }
    }
    return ans + 1;     // 最大的位数为 ans,说明进制数的底线是 ans + 1
}

char N1[20], N2[20], temp[20];
int tag, radix;
int main(){
    init();
    scanf("%s %s %d %d", N1, N2, &tag, &radix);
    if(tag == 2) {      // 交换 N1 N2
        strcpy(temp, N1);
        strcpy(N1, N2);
        strcpy(N2, temp);
    }
    LL t = convertNum10(N1, radix, inf);        // 将 N1 从 radix 进制转化为十进制
    LL low = findLargestDigit(N2);              // 找到 N2 中位数最大的位数加 1,当成二分下界
    LL high = max(low, t) + 1;                  // 上界
    LL ans = binarySearch(N2, low, high, t);    // 二分
    if(ans == -1) printf("Impossible\n");
    else printf("%lld\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/zjsaipplp/p/10425231.html

时间: 2024-07-31 12:02:33

PAT 甲级 A1010 (2019/02/20)的相关文章

PAT 甲级 A1037 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; //段错误,数组开辟的太小 int a[MAXN], b[MAXN]; int main(){ int n1, n2; scanf("%d", &n1); for(int i = 0; i < n1; i++){ scanf("%d", &a[i]); }

PAT 甲级 A1044 (2019/02/20)

#include<cstdio> const int MAXN = 100010; int sum[MAXN]; int n, S, nearS = 100000010; int upper_bound(int L, int R, int x) { int left = L, right = R, mid; while(left < right) { mid = (left + right) / 2; if(sum[mid] > x) { right = mid; } else {

PAT 甲级 A1067 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int a[MAXN]; //存放各个数字当前所处的位置编号 int main(){ int n, num, ans = 0;//表示总计交换次数 scanf("%d", &n); int surplus = n - 1;//存放除0以外不在本位上的数的个数 for(int i = 0; i

PAT 甲级 A1085 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int n, p, a[MAXN]; int binarySearch(int i, long long x){ if(a[n - 1] <= x) //如果最大的数比x小,则返回n return n; int left = i + 1; int right = n - 1; //在区间[i+1, n-1]内查找

PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone c

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a

【PAT甲级】1008 Elevator (20 分)

题意: 电梯初始状态停在第0层,给出电梯要接人的层数和层序号,计算接到所有人需要的时间,接完人后电梯无需回到1层(1层不是0层).电梯上升一层需要6秒,下降一层需要4秒,接人停留时间为5秒. 代码: #include<bits/stdc++.h> using namespace std; int a[100007]; int main(){ int n; cin>>n; int ans=0; for(int i=1;i<=n;++i){ cin>>a[i]; if