您的位置:首页 > 其它

1010. Radix (25)

2016-03-04 09:40 274 查看



时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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、题目简要要求就是给定一个radix进制数字1,和未定进制的数字2,问能否有进制对于数字2,可使得两数字相等。若存在多个对于数字2的进制都可以使得两数字相等,则输出最小的那个进制(此为边界条件)

2、输入数字都以string保存,结果都以unsigned long long 保存。

3、寻找某进制符合要求时使用二分法才能不超时。

4、处理边界条件

#include <iostream>
#include <string>

using namespace std;

int adapter_to_number(char s) //把某个字符转换为数字
{
if (s>='0'&& s<='9')
{
return s-'0';
}else
{
return s-'a'+10;
}
}

unsigned long long pow(unsigned long long radix,unsigned long long num) //求幂运算(用与将一个radix进制数字转换为十进制的函数中)
{
unsigned long long result=1;
if (num==0)
{
return 1;
}else
{
while (num--)
{
result*=radix;
}
}
return result;
}

unsigned long long adapter_to_decimal(string str,unsigned long long radix)//将radix进制的数转换为十进制数(更好的方法是秦九韶的方法)
{
int num=(int)str.size();
unsigned long long sum=0;
for (int i=0; i<str.size(); ++i)
{
sum+=adapter_to_number(str[i])*pow(radix,num-1);
num--;
}
return sum;
}

unsigned long long begin_radix(string str) //题目给出的other数字的最小可能进制
{
char temp='0';
for (int i=0; i<str.size(); ++i)
{
if (str[i]-temp>0)
{
temp=str[i];
}
}
return adapter_to_number(temp)+1;
}

void check(unsigned long long value,string str) //求复合要求的进制,使用二分法,min为value可能的最小进制,max为value本身
{
unsigned long long radix=begin_radix(str);
unsigned long long temp=0;
int flag=0;
unsigned long long min=radix;
unsigned long long max=value;
unsigned long long medium=0;
while (min<=max)
{
medium=(min+max)/2;
temp=adapter_to_decimal(str, medium);
if (temp==value)
{
flag=1;
break;
}else if (temp<value)
{
min=medium+1;
}else
{
max=medium-1;
}
}

if (flag==1) //成功找到进制medium符合要求
{
cout<<medium;
}else //未找到符合要求的进制
{
cout<<"Impossible";
}
}

int main()
{
string str1;
string str2;
int flag=0;
long long radix=0;
cin>>str1>>str2>>flag>>radix;

unsigned long long value_0=0;

if(flag==1)
{
value_0=adapter_to_decimal(str1,radix);
//cout<<value_0;
if (str2.size()==1) //other数字仅有一位才会出现有多个进制都表示同一个数字的情况,此为题目中的提示的边界条件
{
if(adapter_to_number(str2[0])==value_0)//此时other转换为十进制数直接使用adapter_to_number函数
{
cout<<begin_radix(str2);
}else
{
cout<<"Impossible";
}
}else //other不止一位,正常处理
{
check(value_0,str2);
}
}else
{
value_0=adapter_to_decimal(str2,radix);
if (str1.size()==1)
{
if(adapter_to_number(str1[0])==value_0)
{
cout<<begin_radix(str1);
}else
{
cout<<"Impossible";
}
}else
{
check(value_0,str1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: