2020-03-13

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<iostream>
#include<string>
#include<cctype>
#include<math.h>
#include<algorithm>
using namespace std;
int main(){
    long long decimal(string str,long long radix);
    long long find_radix(string str,long long num);
    string m,n;
    long long tag,radix,result;
    cin>>m>>n>>tag>>radix;
    if(tag==1){
        result = decimal(m,radix);
        result = find_radix(n,result);
    }else{
        result = decimal(n,radix);
        result = find_radix(m,result);
    }
    if(result!=-1){
        printf("%lld",result);
    }else{
        printf("Impossible");
    }
    return 0;
}
//将radix进制转为10进制,转化进制有可能溢出,溢出就会返回负数。 
long long decimal(string str,long long radix){
    long long sum = 0,temp = 0;
    int index=0;
    for(int i = str.length()-1;i >= 0;--i){
        if(isdigit(str[i])){
            temp = str[i] - '0';
        }else{
            temp = str[i] - 'a' + 10;
        }
        sum += temp * pow(radix,index++);
    }
    return sum;
}
long long find_radix(string str,long long num){
    char it = *max_element(str.begin(),str.end());//获取字符串中最大的哪一个数。 
    long long low = (isdigit(it) ? it - '0':it - 'a' + 10) + 1;//转换成最小进制情况(里面有9不可能是8进制对吧)。 
    long long high = max(num,low);//进制的最大范围。 
    while(low<=high){
        long long mid = (low + high)/2;
        long long temp = decimal(str,mid);
        if(temp>num || temp<0){//temp为负数,说明大了
            high = mid - 1;
        }else if(temp<num){
            low = mid + 1;
        }else{
            return mid;
        }
    }
    return -1;
} 

ps:网上参考了几位大神解题,用二分法。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,222评论 0 13
  • 我要一位军师来挖掘我的自命 不凡。你
    小小蚂蚁象阅读 1,720评论 0 0
  • “禅修”与我并不陌生,小时记事起,奶奶就是受戒的居士,不时带我拜访庙宇僧人,教我清规戒律和叩拜礼仪。再大一些...
    _汀兰_阅读 1,571评论 0 0
  • 例如: http://www.aspxfans.com:8080/news/index.asp?boardID=5...
    海是倒过来的天_67f2阅读 4,492评论 0 0
  • 露尹 小组长轮流做 王鹏芝 先完成再完美 王彦华 要么读书要么旅游 吴婧 大道至简 天哥 一个人的成功一定要有旺盛...
    mingchuizihuo阅读 1,193评论 0 1

友情链接更多精彩内容