zoukankan      html  css  js  c++  java
  • 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

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<map>
      4 using namespace std;
      5 
      6 map<char,long long> mm;
      7 
      8 long long getlow(char n[])
      9 {
     10     long long Max = -1;
     11     for(long long i = 0 ; i < strlen(n); i++)
     12     {
     13         if(Max < mm[n[i]])
     14             Max = mm[n[i]];
     15     }
     16     return Max+1;
     17 }
     18 
     19 long long getx(char n[],long long radix)
     20 {
     21     long long sum = 0;
     22     for(long long i = 0 ; i < strlen(n);i++)
     23     {
     24         sum = sum * radix + mm[n[i]];
     25     }
     26     return sum;
     27 }
     28 
     29 long long compare(char n[],long long x,long long radix)
     30 {
     31     long long sum = 0;
     32     for(long long i = 0 ; i < strlen(n);i++)
     33     {
     34         sum = sum * radix + mm[n[i]];
     35         if(sum > x ) return 1;
     36     }
     37 
     38     if(sum == x) return 0;
     39     if(sum < x)  return -1;
     40 }
     41 
     42 long long getresult(long long low,long long high,char n[],long long x)
     43 {
     44     //mid  要从low 开始,因为 当两个数相等的时候,它的进制数的最小值是low
     45     long long mid = low ;   
     46     while(low <= high)
     47     {
     48         long long b = compare(n,x,mid);
     49         if(b == 0) return mid;
     50         else if(b == 1)
     51         {
     52             high = mid -1;
     53 
     54         }
     55         else if(b == -1)
     56         {
     57             low = mid+1;
     58         }
     59 
     60         mid = (low + high)/2;
     61     }
     62 
     63     return -1;
     64 }
     65 
     66 int main()
     67 {
     68     long long i;
     69     for(i = '0' ; i <= '9';i++)
     70         mm[i] = i - '0';
     71 
     72     for(i = 'a' ; i <='z' ; i++)
     73         mm[i] = 10 + i - 'a';
     74 
     75 
     76     char n1[21];
     77     char n2[21];
     78     char ctem[21];
     79     long long tag;
     80     long long radix;
     81     scanf("%s%s%lld%lld",n1,n2,&tag,&radix);
     82     if(tag == 2)
     83     {
     84         strcpy(ctem,n2);
     85         strcpy(n2,n1);
     86         strcpy(n1,ctem);
     87     }
     88 
     89     long long x = getx(n1,radix);
     90 
     91     long long low = getlow(n2);
     92     //上限是 x+1 : 10 9999 2 10
     93     //low是 N2某一位上的数,而x 是N1,
     94     //low 某一位上的数就比N1 大,
     95     //那么N2无论什么进制,N1 和 N2肯定是不相等的。
     96     //所以不用判断 low 和 x 的 大小
     97     long long high = x + 1;
     98     long long result = getresult(low,high,n2,x);
     99     if(result == -1) printf("Impossible
    ");
    100     else
    101     {
    102         printf("%lld
    ",result);
    103     }
    104 
    105     return 0;
    106 }
  • 相关阅读:
    更改THttpClientSocket连接超时时间
    咏南跨平台中间件REST API
    INDY10 BASE64编码
    HTTP协议之multipart/form-data
    WWF3动态修改工作流<第九篇>
    WWF3自定义活动<第八篇>
    WWF3追踪功能<WWF第六篇>
    WWF3状态机工作流<WWF第七篇>
    WWF3的持续化<第五篇>
    WWF3事务和异常处理类型活动<第四篇>
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/4304957.html
Copyright © 2011-2022 走看看