您的位置:首页 > 其它

【再思考】1010. Radix (25)

2015-07-10 16:40 363 查看


1010. Radix (25)

时间限制

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

这题确实是我见过的测试用例最多的点,看到通过率真的都吓死了,以为很难的题,做了之后发现,思路可能不是很难,但是测试用例有二十个,能全过的人确实应该很少,下面代码折腾了一个半小时的结果。得分24分

//题目思路:

//1、首先将输出数据用字符串接收,接收后计算它所有进制中最小的进制数;
//2、从最小进制数开始到最大进制,依次转换该数为十进制
//3、专门写一个函数实现任意进制的数向十进制数的转换
//4、将两数都转化为十进制数进行比较
//
//测试点总结:
//1、radix最多可能为多少。一开始我理解错了,以为是只到36,其实目前我的测试用例还有一个不能过,我不知道是不是因为radix的关系,网上说要用二分查找,因为radix可能是一个long long 都存不下的数
//2、进行十进制转化时,要注意字母的特殊处理
//3、注意进制的遍历,要挨个查找,不是直接去最小的进制哦

const long int MAX_radix = 10000;//一开始没理解清楚题意,以为radix最多为36

int FindRadix(string n)//函数实现以字符串形式输出一个数,判断它可能的进制中的最小进制
{
int maxrad = 0;
for (int i = 0; i < n.length(); i++)
{
char c = n[i];
int num = 0;
if (isalpha(c))//判断是否为字母
{
num = c - 87;
}
else
{
num = c - '0';
}
if (num > maxrad)
{
maxrad = num;
}
}
return (maxrad+1);
}

long int ChangtoDecimal(string n, int r)//分别表示该数和该数对应的进制
{
long int sum = 0;
int len = n.length();
for (int i = len - 1; i >= 0; i--)
{
//要注意处理字符串中为字母的情况
char c = n[i];
int bnum = 0;
if (isalpha(c))
{
bnum = c - 87;
}
else
{
bnum = c - '0';
}
sum += bnum*pow(r,len-1-i);
}
return sum;
}

int main()
{
string n1, n2;
int tag, radix;
cin >> n1>>n2>>tag>>radix;
int r1, r2;//分别记录两个数的进制
if (tag == 1)
{
r1 = radix;
r2 = FindRadix(n2);//获得第二个数的进制

int flag = 0;
long int num1 = ChangtoDecimal(n1, r1);//其实这个数有可能很大,long int也是一个测试点
for (long int r = r2; r <= MAX_radix; r++)
{
//将两个数都转化为十进制数进行比较
long int num2 = ChangtoDecimal(n2, r);//一开始没想清楚,应该是任意进制都要进行比较
if (num1 == num2)
{
cout << r;
flag = 1;
break;
}
}
if (flag == 0)
{
cout << "Impossible";
}
}
else if (tag==2)
{
r2 = radix;
r1 = FindRadix(n1);
//将两个数都转化为十进制数进行比较
int flag = 0;
long int num2 = ChangtoDecimal(n2, r2);
for (long int r = r1; r <= MAX_radix; r++)
{
//将两个数都转化为十进制数进行比较
long int num1 = ChangtoDecimal(n1, r);
if (num1 == num2)
{
cout << r;
flag = 1;
break;
}
}
if (flag == 0)
{
cout << "Impossible";
}
}
return 0;
}
【参考】最后的调试,我参考这个人写的分析报告,感觉写的不错http://www.cnblogs.com/549294286/p/3571604.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: