• A1010. Radix


    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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<limits.h>
     5 #include<string.h>
     6 using namespace std;
     7 long long str2num(char s[], long long radix){
     8     long long ans = 0, bit, P = 1;
     9     for(int i = strlen(s) - 1; i >= 0; i--){
    10          bit = (s[i] >= '0' && s[i] <= '9' ) ? (s[i] - '0') : (s[i] - 'a' + 10 );
    11          ans = ans + P * bit;
    12          if(ans < 0)
    13              return -1;
    14          P = P * radix;
    15     }
    16     return ans;
    17 }
    18 long long binSearch(long long low, long long high, char s[], long long x){
    19     long long mid, temp;
    20     while(low < high){
    21         mid = low + (high - low) / 2;
    22         temp = str2num(s, mid);
    23         if(temp > 0 && temp >= x || temp < 0)
    24             high = mid;
    25         else low = mid + 1;
    26     }
    27     return low;
    28 }
    29 int findMax(char s[]){
    30     int max = -1, temp;
    31     for(int i = 0; s[i] != ''; i++){
    32         temp = (s[i] >= '0' && s[i] <= '9' ) ? (s[i] - '0') : (s[i] - 'a' + 10 );
    33         if(temp > max)
    34             max = temp;
    35     }
    36     return max;
    37 }
    38 int main(){
    39     char N1[12], N2[12], temp[12]; 
    40     long long radix, Na, Nb, re;
    41     int tag;
    42     scanf("%s %s %d %lld", N1, N2, &tag, &radix);
    43     if(tag == 2){
    44         strcpy(temp, N1);
    45         strcpy(N1, N2);
    46         strcpy(N2, temp);
    47     }
    48     Na = str2num(N1, radix);
    49     int max = findMax(N2);
    50     re = binSearch(max + 1, INT_MAX, N2, Na);
    51     if(str2num(N2, re) != Na)
    52         printf("Impossible");
    53     else printf("%lld", re);
    54     cin >> tag;
    55     return 0;
    56 }
    View Code

    总结:

    1、题意:给出a进制的N1, 未知进制的N2, 求出这个未知进制,使得a进制的N1 = 未知进制的N2。由于未知进制可能很大,故从可行的最小进制开始递增搜索不可行,会超时。只能用二分法。

    2、当N2位数 >1时,只有一个解。但当N2是1位数时,会有多解。而题目要求输出最小的解,所以二分搜索可以采取寻找第一个使得N2 >= N1的进制。如果它使得N2 = N1,则寻找成功,如果它使得N2 > N1,则输出Impossible。

    3、搜索上界设置为INT_MAX,要注意很大的radix在转换数字时会溢出,所以在mid 转换出的N2与 x 比较时要加上N2 > 0的条件。搜索下界应设置为使N1合法的最小进制,比如N1 = 1234,则下界置为5。

    4、在str2num函数的循环的每一步中都可能出现溢出现象,一旦溢出要直接返回-1,否则有可能后续的转换又会使之变成正数。

    5、int的最大值可以用INT_MAX,需要include<limits.h>。

    6、需要打出一串字符串的结果最好直接复制样例,Impossible打错一个字母硬是有测试点过不去,研究了半天才发现。

  • 相关阅读:
    web前端导出csv文件
    eclipseGUI的可视化开发工具插件
    ionic2程序调试
    rxjs简单入门
    ionic2中使用自定义图标
    解决 Ionic 浏览器跨域问题
    VS2017 Cordova Ionic2 移动开发-环境搭建
    ionic环境搭建
    TypeScript学习笔记 (一)基础特性
    localStorage使用总结
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8510962.html
Copyright © 2020-2023  润新知