题目链接:http://www.patest.cn/contests/pat-a-practise/1010
题目:
时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven 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 10Sample Output 1:2Sample Input 2:1 ab 1 2Sample Output 2:Impossible
分析:
刚开始没用二分查找,而是直接从2遍历到num1 + 1的,出现了超时的情况。
#include<stdio.h>
#include<string.h>
using namespace std;
char str1[11];
char str2[11];
char str_tmp[11];
long long i, j;
long long real_num(char *str, long long r){//找出其十进制的数对应的是多少
long long sum = 0, tmp;
for (i = 0; str[i] != 0; i++){
sum *= r;
if (str[i] >= '0' && str[i] <= '9')
tmp = str[i] - '0';
else if (str[i] >= 'a' && str[i] <= 'z')
tmp = str[i] - 'a' + 10;
if (tmp >= r)return -1;
//当位数上的数字大于进制时,是不可能的
sum += tmp;
}
return sum;
}
int main(void){
long long tag, radix;
long long num1, num2;
while (scanf("%s %s %lld %lld", str1, str2, &tag, &radix) != EOF){
if (strcmp(str1, str2) == 0){
printf("%lld\n", radix);
continue;
}//如果两个字符串相同,说明进制相同。
if (tag == 2){
strcpy(str_tmp, str2);
strcpy(str2, str1);
strcpy(str1, str_tmp);
}//如果tag为2,那么交换str1和str2,相当于都变为tag为1的情况。
num1 = real_num(str1, radix);
/* long long base = 1, top = num1 + 1, mid;
while (base <= top){
mid = (base + top) / 2;
num2 = real_num(str2, mid);
if (num2 == num1)break;
else if (num2 > num1)top = mid - 1;
else base = mid + 1;
}//这里要用二分查找,不然会有超时的情况
if (num1 == num2)printf("%lld\n", mid);
else { */
long long ans = -1;
for (j = 2; j < num1 + 2; j++){
long long num2 = real_num(str2, j);
if (num2 == num1){
ans = j;
break;
}
//这里现在只用了遍历
else if (num2 > num1)break;
}
if (ans == -1)puts("Impossible");
else printf("%lld\n", ans);
}
// }
return 0;
}
#include<stdio.h>
#include<string.h>
using namespace std;
char str1[11];
char str2[11];
char str_tmp[11];
long long i, j;
long long real_num(char *str, long long r){//找出其十进制的数对应的是多少
long long sum = 0, tmp;
for (i = 0; str[i] != 0; i++){
sum *= r;
if (str[i] >= '0' && str[i] <= '9')
tmp = str[i] - '0';
else if (str[i] >= 'a' && str[i] <= 'z')
tmp = str[i] - 'a' + 10;
if (tmp >= r)return -1;
//当位数上的数字大于进制时,是不可能的
sum += tmp;
}
return sum;
}
int main(void){
long long tag, radix;
long long num1, num2;
while (scanf("%s %s %lld %lld", str1, str2, &tag, &radix) != EOF){
if (strcmp(str1, str2) == 0){
printf("%lld\n", radix);
continue;
}//如果两个字符串相同,说明进制相同。
if (tag == 2){
strcpy(str_tmp, str2);
strcpy(str2, str1);
strcpy(str1, str_tmp);
}//如果tag为2,那么交换str1和str2,相当于都变为tag为1的情况。
num1 = real_num(str1, radix);
long long base = 1, top = num1 + 1, mid;
while (base <= top){
mid = (base + top) / 2;
num2 = real_num(str2, mid);
if (num2 == num1)break;
else if (num2 > num1)top = mid - 1;
else base = mid + 1;
}//这里要用二分查找,不然会有超时的情况
if (num1 == num2)printf("%lld\n", mid);
/* else {
long long ans = -1;
for (j = 2; j < num1 + 2; j++){
long long num2 = real_num(str2, j);
if (num2 == num1){
ans = j;
break;
}
else if (num2 > num1)break;
}
if (ans == -1)
*/
else puts("Impossible");
// else printf("%lld\n", ans);
//}
}
return 0;
}
#include<stdio.h>
#include<string.h>
using namespace std;
char str1[11];
char str2[11];
char str_tmp[11];
long long i,j;
long long real_num(char *str,long long r){//找出其十进制的数对应的是多少
long long sum = 0,tmp;
for(i = 0; str[i] != 0; i ++){
sum *= r;
if(str[i] >= '0' && str[i] <= '9')
tmp = str[i] - '0';
else if(str[i] >= 'a' && str[i] <= 'z')
tmp = str[i] - 'a' + 10;
if(tmp >= r)return -1; //当位数上的数字大于进制时,是不可能的
sum += tmp;
}
return sum;
}
int main(void){
long long tag,radix;
long long num1,num2;
while(scanf("%s %s %lld %lld",str1,str2,&tag,&radix) != EOF){
if(strcmp(str1,str2) == 0){
printf("%lld\n",radix);
continue;
}//如果两个字符串相同,说明进制相同。
if(tag == 2){
strcpy(str_tmp, str2);
strcpy(str2, str1);
strcpy(str1, str_tmp);
}//如果tag为2,那么交换str1和str2,相当于都变为tag为1的情况。
num1 = real_num(str1, radix);
long long base = 1,top = num1 + 1,mid;
while(base <= top){
mid = (base + top) / 2;
num2 = real_num(str2,mid);
if(num2 == num1)break;
else if(num2 > num1)top = mid -1;
else base = mid + 1;
}//这里要用二分查找,不然会有超时的情况
if(num1 == num2)printf("%lld\n",mid);
else {
long long ans = -1;
for(j = 2; j < num1 + 2; j ++){
long long num2 = real_num(str2,j);
if(num2 == num1){
ans = j;
break;
}//再遍历一遍。
else if(num2 > num1)break;
}
if(ans == -1)puts("Impossible");
else printf("%lld\n",ans);
}
}
return 0;
}