1010 Radix (25 分)

1010 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 N​1​​ and N​2​​, 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

代码

自己代码22分

#include <iostream>
using  namespace std;

long long getNum(string str,long long radix){
    
    
	//作用:按照基数radix,将字符串转化str为数
	//可能会很大,用longlong
    long long sum=0;
    for (int i = 0; i < str.length(); ++i) {
    
    
        if (str[i] <= '9') {
    
    
        	sum = sum * radix + (str[i] - '0');
		}
        else if(str[i]<='z') {
    
    
        	sum = sum*radix+(str[i]-'a'+10);
		}
    }
    return sum;
}
int main() {
    
    
    string str1,str2;
    int tag;
    long long radix,num1;
    cin >> str1 >> str2 >> tag >> radix;
    
    if(tag == 1){
    
    
        num1=getNum(str1,radix);
    }
    else{
    
     // 如果tag=2,则将str1和str2交换 
        num1 = getNum(str2,radix);
        str2 = str1;
    }
    
    bool flag = false;
    for(int i=2; i<num1; i++) {
    
    
    	if(getNum(str2,i) == num1) {
    
    
    		cout << i;
    		flag = true;
    		break;
		}
	}
    
    if(!flag){
    
    
        cout << "Impossible";
    }

    return 0;
}

满分代码

#include <iostream>
using  namespace std;

long long getNum(string str,long long radix){
    
    
	//作用:按照基数radix,将字符串转化str为数
	//可能会很大,用longlong
    long long sum=0;
    for (int i = 0; i < str.length(); ++i) {
    
    
        if (str[i] <= '9') {
    
    
        	sum = sum * radix + (str[i] - '0');
		}
        else if(str[i]<='z') {
    
    
        	sum = sum*radix+(str[i]-'a'+10);
		}
    }
    return sum;
}
long long getRadix(long long num1,string str,long long start,long long end){
    
    
    if(start==end){
    
    
    	//等号单独讨论,否则可能死循环
        if(getNum(str,start)==num1) {
    
    
        	return start;
		}
        else{
    
     
			return 0;
		}
    }
    else if(start<end){
    
    
        long long radix=(start+end)/2;
        //二分查找
        if(getNum(str,radix) == num1) {
    
    
			return radix;
		}else if(getNum(str,radix)>num1 || getNum(str,radix)<0) {
    
    //num2>num1或num2<0都表示这个radix取得过大了,需要减小
			return getRadix(num1,str,start,radix);
		}else {
    
    //num2<num1,说明radix取得比较小,所以要往大了取
			return getRadix(num1,str,radix+1,end);
		} 
    }
    return 0;
}
int getMinRadix(string num2){
    
    
	//作用:找到num2的最小基数
	//例如:num2中最大的权值为b时,最小基数就是b+1=12
    char max='0';
    for (int i = 0; i < num2.length(); ++i) {
    
    
    	if(num2[i] > max) {
    
    
    		max = num2[i];
		}
	}
    if (max <= '9') {
    
    
    	return max-'0'+1;
	}
    return max-'a'+11;
}
int main() {
    
    
    string str1,str2;
    int tag;
    long long radix,num1;
    cin >> str1 >> str2 >> tag >> radix;
    
    if(tag == 1){
    
    
        num1=getNum(str1,radix);
    }
    else{
    
     // 如果tag=2,则将str1和str2交换 
        num1 = getNum(str2,radix);
        str2 = str1;
    }
    
    int res = getRadix(num1, str2, getMinRadix(str2), num1+1);
    
    if(res == 0){
    
    
        cout << "Impossible";
		return 0;
    }
    
    cout << res;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44635198/article/details/114302884