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


题目:进制转换,4个输入,输出可以把一个数转换成另一个数的进制,没有则输出impossible


坑:

1.中间的变量用int保存可能溢出

2.注意时间效率,可能会超时

3.一些特殊情况


填坑的方法:

第一个坑的对策:

注意数据类型, 简单的int保存中间结果的十进制数会溢出,看了博文,用long long 或者double,顺便整理一下基本知识:

数据类型 表示范围

int 32位(新版的环境应该不会遇到16位?) -2^31 ~ 2^31-1

long long 64位 -2^63 ~ 2^63 -1

第二个坑:

二分法,遍历不行,会超时

第三个坑:

当非tag的输入只有一位的时候,比如: a 1010 2 12(tag 是2 指向1010, 而a只有一位)

当心题设中的条件,每一位的digit要大于radix! 比如输入是a, 那么输出的radix最少得是11,这个比较容易忽视!!!


Code:

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>


int main() {
double max = pow(35, 10);
std::string input[2];
int tag;
int radix;
std::cin >> input[0];
std::cin >> input[1];
std::cin >> tag;
std::cin >> radix;
tag--;
int other_tag = (tag == 1 ? 0: 1);  // if tag == 0 other_tag=1 else other_tag =0
double result;
bool is_find = false;  // record if the result is found
double decimal_num = 0.0;  // decimal vaule of the input[tag]
double max_digit = -1;  // max digit of the input[other_tag]


// calculate the decimal_num
for (int i = 0; i < input[tag].size(); i++) {
 double coh = ((input[tag][i] - '0') > 48 ? input[tag][i] - 'a' + 10 : input[tag][i] - '0');
 decimal_num += pow(radix, input[tag].size() - 1 - i) * coh;
}


// find the max_digit
for (int i = 0; i < input[other_tag].size(); i++) {
double coh = ((input[other_tag][i] - '0') > 48 ? input[other_tag][i] - 'a' + 10 : input[other_tag][i] - '0');
if (max_digit < coh) {
max_digit = coh;
}
}


// specail handle: if the input[other_tag] has only one digit
if (input[other_tag].size() == 1) {
int num = (input[other_tag][0] - '0') > 48 ? input[other_tag][0] - 'a' + 10 : input[other_tag][0] - '0';
if (abs(decimal_num - num) < 0.01) {  // equal with decimal_num
printf("%.0f", max_digit+1);
} else {
printf("Impossible");
}
system("pause");
return 0;
}


// Bisearch
double low = 2;
double high = max;
double test_radix = floor((low + high) / 2);
while (high > low) {
double temp = test_radix;
double other_tag_decimal_num = 0.0; // decimal vaule of input[other_tag]


// calculate other_tag_decimal_num
for (int i = 0; i < input[other_tag].size(); i++) {
double coh2 = ((input[other_tag][i] - '0') > 48 ? input[other_tag][i] - 'a' + 10 : input[other_tag][i] - '0');
other_tag_decimal_num += pow(test_radix, input[other_tag].size() - 1 - i) * coh2;
if (other_tag_decimal_num > decimal_num) {  // once greater than decimal_num, break
high = test_radix;
break;
}
}
if (other_tag_decimal_num < decimal_num) {  // less than decimal_num, test_radix should enlarge
low = test_radix;
}
else if (other_tag_decimal_num - decimal_num < 0.01) {  // equal, record and continue
high = test_radix;
if (test_radix > max_digit) {  // fliter the illegal results
result = test_radix;
is_find = true;
}
}
test_radix = floor((low + high) / 2);
if (temp == test_radix) {
break;
}
}
if (is_find) {
printf("%.0f", result);
}
else {
printf("Impossible");
}
system("pause");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值