PAT甲级 1010.Radix(25) 题目翻译与答案

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/qq278672818/article/details/56690015

题目来源自PAT网站  https://www.patest.cn/

题目描述:

1010. Radix (25)

iven a pair ofpositive 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 binarynumber.

Now for any pair ofpositive integers N1 and N2, your task is to find the radix of one number whilethat of the other is given.

InputSpecification:

Each input filecontains one test case. Each case occupies a line which contains 4 positiveintegers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radixand is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers0-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.

OutputSpecification:

For each test case,print in one line the radix of the other number so that the equation N1 = N2 istrue. If the equation is impossible, print "Impossible". If thesolution is not unique, output the smallest possible radix.

SampleInput 1:

6 110 1 10

SampleOutput 1:

2

SampleInput 2:

1 ab 1 2

SampleOutput 2:

Impossible

题目翻译:

1010.基数

对于一对整数,例如6和110,6=110这个等式是正确的么?如果6是十进制数而110是二进制数时,答案是“yes”。

现在对于每对整数N1和N2,你的任务是当一个数的基数已经给你时,你找出另一个数字的基数。

 

输入说明:

每个输入文件包含一个测试实例。每个实例包含一行,里面有4个正整数:N1 N2 tag radix

在这里N1和N2不会超过10个数字。数字是小于的它的基数的,而且是从{0-9, a-z}这个集合中选择。其中0-9代表十进制数0-9,a-z代表十进制数10-35。如果“tag”为1,则最后一个数字“radix”代表N1的基数;如果“tag”为2,则代表N2的基数。

#include<cstdio>
char N1[11]="\0",N2[11]="\0";
long long calValue(char N[],long long radix);
int minradix(char N[]);
long long judge(long long n1,char N[],int minrad);
int main()
{
	long long n1,n2;
	long long tag,radix1,radix2,minrad;
	long long out;
	scanf("%s",N1);
	scanf("%s",N2);
	scanf("%lld",&tag);
	if(tag==1)
	{
		scanf("%lld",&radix1);
		n1=calValue(N1,radix1);
		if(n1<0)
			out=0;
		else 
			minrad=minradix(N2);
		out=judge(n1,N2,minrad);
	}else
	{
		scanf("%lld",&radix2);
		n2=calValue(N2,radix2);
		if(n2<0)
			out=0;
		else 
			minrad=minradix(N1);
		out=judge(n2,N1,minrad);
	}
	if(out==0)
		printf("Impossible\n");
	else
		printf("%lld\n",out);
	return 0; 
}
long long calValue(char N[],long long radix)
{
	long long sum=0;
	long long i=0;
	while(N[i]!='\0')
	{
		if(N[i]>='0'&& N[i]<='9')
			sum=sum*radix+(N[i]-'0');
		else
			sum=sum*radix+(N[i]-'a'+10);
		if(sum<0)
			return -1;
		i++;
	}
	return sum;
}
int minradix(char N[])
{
	int i,Max=0;
	for(i=0;N[i]!=0;i++)
		if(N[i]>Max)
			Max=N[i];
	if(Max>='0'&& Max<='9')
		return Max-'0';
	else
		return Max-'a'+10; 
}
long long judge(long long n1,char N[],int minrad)
{
	unsigned long long n2;
	unsigned long long beg=minrad+1,end=n1;
	if(beg>end)
		end=beg;
	unsigned long long mid=(beg+end)/2;
	while(mid>=beg && mid<=end)
	{
		n2=calValue(N,mid);
		if(n2<0)
			return 0;
		if(n2==0 && n2==n1)
			return 1;
		if(n1==n2)
			return mid;
		if(n2>n1)
			end=mid-1;
		else
			beg=mid+1;
		mid=(beg+end)/2;
	}
	return 0;
}

 

输出说明:

对于每个测试用例,在一行中输出使得N1=N2这个等式成立的另一个数的基数。如果等式不可能成立,输出"Impossible"。如果答案不唯一,输出最小可能的基数。

 

样例1输入:

6 110 1 10

 

样例1输出:

2

 

样例2输入:

1 ab 1 2

样例2输出:

Impossible

答案代码:



说明与心得:

对于judge函数,一开始我从midrax+1一直循环到某个数,最高只能23分。

后根据http://www.liuchuo.net/archives/2458采用二分法,可是最高也只能得23分。

后来各种改数据类型,long long 与unsigned long long,最后得以通过。



展开阅读全文

没有更多推荐了,返回首页