1010. Radix (25)

本文介绍了一个基数转换的问题,即给定两个正整数N1和N2,在已知其中一个数的基数的情况下,找到另一个数的基数使得这两个数相等。文章提供了两种不同的实现方式,并讨论了特殊情况的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

分析:看似简单的一道题,折磨死我了。一开始很多测试点过不了,看了网上的提示是需要使用long long型,然后需要二分查找提高效率,不然有一个测试点过不了。

还有几种特殊情况要考虑,最小进制为2.后面顺序查找有一个测试点过不了。

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

long long int Cal(char c)
{
	if (c >= '0' && c <= '9')
	{
		return (c - '0');
	}
	else
		return (c - 'a' + 10);
}

long long int CalValueBaseRadix(char t[], long long int radix)
{
	int i;
	long long int result = 0;
	for (i = 0; i<strlen(t); i++)
	{
		result *= radix;
		result += Cal(t[i]);
		if (result < 0)
		{
			return -1;
		}
	}
	return result;
}

int main(void)
{
	

	char a[15], b[15];
	long long tag;
	long long radixA, radixB;

	scanf("%s %s %lld %lld", a, b, &tag, &radixA);

	if (tag == 2)
	{
		//swap a and b    
		char tmp[15];
		strcpy(tmp, a);
		strcpy(a, b);
		strcpy(b, tmp);
	}

	long long minRadix, maxRadix;

	//value    
	long long i;
	long long int valueA = CalValueBaseRadix(a, radixA);

	minRadix = 0;
	for (i = 0; i<strlen(b); i++)
	{
		if (Cal(b[i]) > minRadix)
			minRadix = Cal(b[i]) + 1;
	}


	maxRadix = valueA + 1;
	while (minRadix <= maxRadix)
	{
		//cal    
		radixB = (minRadix + maxRadix) / 2;
		long long int valueB = CalValueBaseRadix(b, radixB);

		if (valueB == -1 || valueB > valueA)
		{
			maxRadix = radixB - 1;
		}
		else if (valueB < valueA)
		{
			minRadix = radixB + 1;
		}
		else if (valueA == valueB)
		{
			printf("%lld",radixB);
			return 0;
		}

	}

	printf( "Impossible");
	
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char basic[36] = { '0','1','2','3','4','5','6','7','8','9',
                   'a','b','c','d','e','f','g','h','i','j',
                   'k','l','m','n','o','p','q','r','s','t',
                   'u','v','w','x','y','z'};

long long int Todec(char* num, int radix)
{
	int bas,temp;
	int i=0;
         long long sum=0;
	
	for (i = 0; i < strlen(num)-1; i++)
	{  if (num[i] >= '0'&&num[i] <= '9')temp = num[i] - '0';
	   else temp = num[i] - 'a' + 10;
	   sum = (sum + temp)*radix;
	}
	if (num[i] >= '0'&& num[i] <= '9')temp = num[i] - '0';
	else temp = num[i] - 'a' + 10;
	sum += temp;
	return sum;
}
int Findmax(char* num)
{
	int i,temp;
	char max = '0';
	for (i = 0; i < strlen(num); i++)
	{
		if (num[i] > max)
			max = num[i];
	}
	if (max >= '0'&&max <= '9')temp = max - '0';
	else temp = max - 'a' + 10;
	return temp;
}
long long int Findradix(char* num, long long int sum)
{
	int max = Findmax(num);
	int temp,i;
        long long index=-1;
	for (i = max+1; i < sum+2; i++)
		if (sum == Todec(num, i))
		{
			index = i;
			break;
		}
	
    return index;
}
int main()
{
	
	char num1[11] ,num2[11];
	int radix;
	int flag;
	long long int i;
	long long int result ;
	scanf( "%s", num1);
	scanf( "%s", num2);
	scanf( "%d", &flag);
	scanf( "%d", &radix);
	if (flag == 1)
	{
		i = Todec(num1, radix);
		result = Findradix(num2, i);
	}
	else
	{
		i = Todec(num2, radix);
		result = Findradix(num1, i);
	}
	if (result == -1)
		printf("Impossible");
	else if (result < 2)
		printf("2");
	else
		printf("%d", result);
	
	return 0;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值