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
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long LL;
    LL Map[257];
    LL inf = (1LL << 63) - 1;
    
    void init(){
        for(char c = '0'; c <= '9'; c++){
            Map[c] = c - '0';
        }
        for(char c = 'a'; c <= 'z'; c++){
            Map[c] = c - 'a' + 10;
        }
    }
    
    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;
        }
        return ans;
    }
    
    int cmp(char N2[],LL radix, LL t){
        LL num = convertNum10(N2,radix,t);
        if(num < 0) return 1; //代表mid太大了
        if(num == t) return 0;
        else if(num > t) return 1;
        else return -1; 
    }
    
    int binarySearch(char N2[],LL left,LL right,LL t){
        LL mid;
        while(left <= right){
            mid = (left+right)/2;
            int flag = cmp(N2,mid,t);
            if(flag == 0 ) return mid;
            if(flag == 1) right = mid - 1;
            else left = mid + 1;
        }
        return -1;
    }
    
    LL findLargest(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;
    }
        char N1[20],N2[20],temp[20];
        int tag,radix;
    int main(){
        init();
    //    char N1[20],N2[20],temp[20];
    //    LL tag,radix;
        scanf("%s %s %d %d",N1,N2,&tag,&radix);
        if(tag == 2){
            strcpy(temp,N1);
            strcpy(N1,N2);
            strcpy(N2,temp);
        }
        LL t = convertNum10(N1,radix,inf);
        LL low = findLargest(N2);
        LL high = max(t,low) + 1;
        LL ans = binarySearch(N2,low,high,t);
        if(ans == -1) printf("Impossible
    ");
        else printf("%lld
    ",ans);
        return 0;
    }

     20190730

    //如果使用int会有三个点过不去
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn = 20;
    const int INF = 100000000;
    int map[257] = {0};
    
    int init()
    {
        for(char c = '0'; c <= '9'; c++)
        {
            map[c] = c - '0';
        }
        for(char c = 'a'; c <= 'z'; c++)
        {
            map[c] = c - 'a' + 10;
        }
    }
    
    int Convert2Num(char *str,int radix,int Max)
    {
        int num = 0;
        int len = strlen(str);
        for(int i = 0; i < len; i++)
        {
            num = num * radix + map[str[i]];
            if(num < 0 || num > Max) return -1;
        }
        return num;
    }
    
    int FindLargest(char *str)
    {
        int len = strlen(str);
        int iMax = -1;
        for(int i = 0; i < len; i++)
        {
            if(iMax < map[str[i]])
            {
                iMax = map[str[i]];
            }
        }
        return iMax + 1;
    }
    
    //1 mid is bigger
    //-1 mid is smaller 
    int cmp(char *N2,int radix,int N1Num)
    {
        int N2Num = Convert2Num(N2,radix,N1Num);
        if(N2Num < 0 ) return 1;
        if(N2Num > N1Num) return 1;
        else if(N2Num < N1Num) return -1;
        else return 0;
    }
    
    int Binary(char *N2,int low,int high,int N1Num)
    {
        int mid = 0;
        while(low <= high)
        {
            mid = (low + high) / 2;
            int ans = cmp(N2,mid,N1Num);
            if(1 == ans) high = mid - 1;
            else if(-1 == ans) low = mid + 1;
            else return mid; 
        } 
        return -1;
    }
    
    int main()
    {
        init();
        char N1[maxn],N2[maxn],temp[maxn];
        int radix,tag;
        scanf("%s%s%d%d",N1,N2,&tag,&radix);
        if(2 == tag)
        {
            strcpy(temp,N1);
            strcpy(N1,N2);
            strcpy(N2,temp);
        }
        int N1Num = Convert2Num(N1,radix,INF);
        int low = FindLargest(N2);
        int high = max(N1Num,low) + 1;
        int ans = Binary(N2,low,high,N1Num);
        if(-1 == ans) printf("Impossible
    ");
        else printf("%d",ans);
        return 0;
    }
    //还有10号点过不去 
    //没过是因为有两个函数的返回值是int
    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 12; //const int INF = 100000000; LL INF = (1LL << 63) - 1; LL map[257] = {0}; void init() { for(char c = '0'; c <= '9'; c++) { map[c] = c - '0'; } for(char c = 'a'; c <= 'z'; c++) { map[c] = c - 'a' + 10; } } int Convert2Num(char *str,LL radix,LL Max) { LL num = 0; int len = strlen(str); for(int i = 0; i < len; i++) { num = num * radix + map[str[i]]; if(num < 0 || num > Max) return -1; } return num; } int FindLargest(char *str) { int len = strlen(str); int iMax = -1; for(int i = 0; i < len; i++) { if(iMax < map[str[i]]) { iMax = map[str[i]]; } } return iMax + 1; } //1 mid is bigger //-1 mid is smaller int cmp(char *N2,LL radix,LL N1Num) { LL N2Num = Convert2Num(N2,radix,N1Num); if(N2Num < 0 ) return 1; if(N2Num > N1Num) return 1; else if(N2Num < N1Num) return -1; else return 0; } int Binary(char *N2,LL low,LL high,LL N1Num) { LL mid = 0; while(low <= high) { mid = (low + high) / 2; int ans = cmp(N2,mid,N1Num); if(1 == ans) high = mid - 1; else if(-1 == ans) low = mid + 1; else return mid; } return -1; } int main() { init(); char N1[maxn],N2[maxn],temp[maxn]; int radix,tag; scanf("%s%s%d%d",N1,N2,&tag,&radix); if(2 == tag) { strcpy(temp,N1); strcpy(N1,N2); strcpy(N2,temp); } LL N1Num = Convert2Num(N1,radix,INF); LL low = FindLargest(N2); LL high = max(N1Num,low) + 1; LL ans = Binary(N2,low,high,N1Num); if(-1 == ans) printf("Impossible "); else printf("%lld",ans); return 0; }
  • 相关阅读:
    phpstorm插件等相关推荐
    Item Pipeline
    没有返回值的构造函数是怎么完成赋值的?
    vue中如何实现点击变成全屏
    vue操作dom元素的三种方法
    陈同学整理的10个可以写到简历上C++项目
    从四个问题透析Linux下C++编译&链接
    C++隐式推导-auto关键词
    从今天起构建你的JavaScript世界
    vue中实现模态框弹出框动画(旋转弹出)
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/9408281.html
Copyright © 2011-2022 走看看