天天看點

1010 Radix(25 分)

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 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
           

轉載來自大佬:https://blog.csdn.net/CV_Jason/article/details/80993283

代碼:

#include<iostream>
#include<cctype>
#include<algorithm>
#include<cmath>
using namespace std;
long long str2num(string str,int radix){
    long long sum = 0;
    int index = 0;
    int per_digit = 0;
    for(auto t = str.rbegin();t!=str.rend();t++){
        per_digit = isdigit(*t)? *t - '0':*t - 'a' + 10;
        sum+=per_digit * pow(radix,index++);
    }
    return sum;
}
long long find_radix(string str,long long num){
    long long result_radix = -1;
    char it = *max_element(str.begin(),str.end());
    long long low = (isdigit(it)?it - '0':it - 'a' + 10) + 1;
    long long high = max(low,num);
    while(low<=high){
        long long mid = (low+high)/2;
        long long temp = str2num(str,mid);
        if(temp<0||temp>num){
            high = mid - 1;
        }else if(temp<num){
            low = mid + 1;
        }else{
            result_radix = mid;
            break;
        }
    }
    return result_radix;
}

int main(){
    string N1;
    string N2;
    int tag;
    long long radix;
    while(cin>>N1>>N2>>tag>>radix){
        long long known_num = (tag==1?str2num(N1,radix):str2num(N2,radix));
        long long result = find_radix((tag==1?N2:N1),known_num);
        if(result!=-1)
            cout<<result<<endl;
        else
            cout<<"Impossible"<<endl;
    }
    return 0;
}           

我自己的寫的代碼不知道怎麼說...... 是不對

代碼:

1010 Radix(25 分)
#include <iostream>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
vector<long long>vec;
//轉化成十進制的ll 
ll Strtonum(string s, ll radix)
{
	ll sum = 0; 
	ll radix_num = 1;
	string t = s;
	reverse(t.begin(),t.end());//反轉下
	for(int i = 0 ; i < s.size(); i++)
	{
		ll kk;
		if(isdigit(t[i]))
			kk = t[i] - '0';
		else 
			kk = t[i] - 'a' + 10;	
		sum += radix_num * kk;
		radix_num = radix_num * radix;
	} 
	return sum;
}
ll findMaxchar(string s)
{
	ll maxn = 0;
	for(int i = 0 ; i < s.size(); i++)
	{
		ll kk;
		if(isdigit(s[i]))
			kk = s[i] - '0';
		else 
			kk = s[i] - 'a' + 10;
		maxn = max(kk, maxn);//求出最大的那個 
	}
	return maxn; 
}
int main()
{
	string n1,n2;
	ll tag,radix,maxn1,maxn2;
	cin>>n1>>n2>>tag>>radix;
	//進制有個條件就是......??每個數字都不能超過....該進制
	if(tag == 1)
		maxn1 = findMaxchar(n1);
	else 
		maxn1 = findMaxchar(n2);
	
	if(maxn1 >= radix)
	{
		printf("Impossible\n");
		return 0;
	}
	ll num1 = tag == 1? Strtonum(n1,radix):Strtonum(n2,radix);
	ll left = 2, right = 50, mid;
	while(left <= right)
	{
		ll mid = (left + right) / 2; //假裝mid就是進制
		ll num2 = tag == 1? Strtonum(n2,mid):Strtonum(n1,mid);
		if(num2 == num1)
		{
			vec.push_back(mid);
			right = mid - 1;	
		}else if(num2 > num1)
			right = mid - 1;
		else if(num2 < num1)
			left = mid + 1;
	} 
	if(tag == 1)
		maxn2 = findMaxchar(n2);
	else 
		maxn2 = findMaxchar(n1);
	if(vec.size() == 0) 
		printf("Impossible\n");
	else if(maxn2 >= vec[vec.size() - 1])
		printf("Impossible\n");
	else 
		printf("%lld\n",vec[vec.size() - 1]);
	return 0;	
}