pat1010

19 篇文章 0 订阅

1010. Radix (25)

时间限制   400 ms   内存限制   32000 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


代码还有一点问题,先去吃饭,回来的时候重新整理一下思路。

#include <cstdio>
#include <string.h>
#include <cstdlib>

#define a 10 
#define MAXN 15

using namespace std ;

char num1[MAXN], num2[MAXN];
char temp[MAXN] ;
char tempX [MAXN] ;
int radix , tag ;
int num_10 = 0 ;

int getPow ( int radix , int n )
{
	int result = 1 ;

	if ( n == 0 )
		return 1 ;
	else
	{
		for ( int i = 1 ; i <= n ; i++ )
		{
			
			result *= radix ;
		}
	}
	return result; 
}
void inPut ()
{
	scanf("%s",num1  ) ;
	scanf("%s", num2) ;
	scanf("%d", &tag) ;
	scanf("%d", &radix ) ;
	
	
	if ( tag == 1 )
	{
		strcpy( temp , num1 ) ;
		strcpy( tempX , num2 ) ;
	}
	else if ( tag == 2 )
	{
		strcpy( temp, num2 ) ;
		strcpy(tempX , num1  ) ;
	}

	
	int len = strlen(temp) ;


	for (int i = len-1 ; i >= 0 ; i-- )
	{
		if ( temp[i] >='0' && temp[i] <='9' )
		{
		num_10 +=  getPow(radix, len - i- 1)*(temp[i]-'0') ;
	
		}
		else if (temp[i] >= 'a' && temp[i] <= 'z')
		{
			num_10 += (temp[i] -'a'+10)*getPow(radix , len - i-1 ) ;
		}

	}
}

int getDigit(  int digit )
{
	
	int len = strlen ( tempX ) ;
	
	int result = 0 ;

	for ( int i = 0 ; i < len ; i++ )
	{
		int t ;

		if ( tempX[i]>= '0' && tempX[i] <= '9' )
		{
			t = tempX[i]-'0';
		}
		else if ( tempX[i] >= 'a' && tempX[i] <= 'z' )
		{
			t = tempX[i]-'a'+10 ;
		}

		result += getPow(digit , len -i -1)*t ;
		//printf("here is the result of the getPow : %d \n", getPow( digit , len-i-1)*t) ;
		//printf("here is the result of this time %d \n", result ) ;
	}
	return result ;
}

int main ( void )
{
	int digit = -1 ;

	inPut () ;

	//printf("here is the num_10 %d\n", num_10) ;
	for ( int i = 2 ; i <= 35 ; i++ )
	{
		if (num_10 == getDigit(i))
		{
			digit = i ;
			break ;
		}
		//printf("here is the digit %d \n", getDigit(i)) ;
	}

	if ( digit != -1 )
	printf("%d",digit ) ;
	else
		printf("Impossible") ;

//	system("pause") ;

	return 0 ; 
}

上面的代码所得分数是 17/25

下面的代码所得分数是 24/25 一个测试用例没过,找了一下午,还没有找出来,先去吃饭,回来再写

下面的代码主要从两个地方进行改进, 突然体会到,题中的每一句话并不是白说的,

比如说,题目中有这样的一句描述: 如果数值相等的话,那么输出最小的哪一个进制,

仔细分析以后 如果 对应的10 进制的 6  与 x 进制的 6, 如果按照原先的思想的话, 那么数值 6 以 1 进制的条件也是满足的,

但是事实并非如此,也就是说,对于一个字符串所描述的数字中,最大的数据位为 X 的话,那么对于这个数字,它的最小的进制一定是  X+1 

就拿 10101 码来说吧,最大出现的数值是  1 , 那么表示这段数字的最小进制数目一定是 2 进制。

 所以添加了一个方法 getBin () 这个方法会返回,出现在字符串所描述的数位中最大数据的数值,

比如说  char  list [] ={'a', 'c' '1' } ;  a ->10 , c -> 12  那么这个 getBin 方法会返回的数值就是 12 ,

在原先调用方法中进行返回值 +1 的操作, 即可推知, 满足 ac1 表示方法的最小进制,必定是13 进制或是大于 13 进制的表示方式。


所以根据这个条件的了1 分。

然后又对循环中  i<= 35 的地方进行扩大范围,说实话这个地方我不太明白是什么意思, 但是扩大范围之后确实有通过好几个测试点。


按理说,a-z 对应的数值分别是10 -> 35 ,这也就是说,最大的进制必定是不会超过37 的,不然的话, 数值36 占用了2个数位,

数值 37 也是占用了 2 个数位应该如何表述呢? 总之,先去吃饭吧~


#include <cstdio>
#include <cstdlib>
#include <string.h>


char num[2][11] ;
int tag ,bin_ ;

long getPow( long x , int n )
{
	if ( n == 0 )
		return 1 ;
	else
		return x*getPow( x , n-1 ) ;

}

long getNum ( char list[] ,int bin)
{
	int len = strlen(list) ;
	
	long num = 0 ;

	for ( int i = len -1 ; i >= 0 ; i-- )
	{
		
		if ( list[len-i-1]>= 'a' && list[len-i-1] <= 'z' )
		{
			num += ((list[len - i - 1 ]-'a')+10)*getPow( bin,i ) ;
		}
		else if ( list[len-i-1]>='0' && list[len-i-1] <= '9' )
		{
			num += (list[len-i-1] -'0')*getPow( bin , i ) ;
		}

	}

	return num ;
}

long getBin ( char list[] )
{
	int b = 1 ,temp ;
	int len = strlen ( list ) ;

	for ( int i = 0 ; i < len ; i++ )
	{
		if (list[i]>='0'&&list[i]<='9')
		{
			temp = list[i]-'0' ;
		}
		else if ( list[i]>= 'a' && list[i] <= 'z' )
		{
			temp = list[i]-'a'+10 ;
		}

		if ( temp > b )
		{
			b = temp ;
		}

	}

	return b ;

}

void inPut ()
{
	scanf("%s%s", num[0],num[1]) ;
	scanf("%d%d", &tag, &bin_) ;

}

int main ( void )
{
	long Tbin ;
	int x = -1 ;
	int b ;

	inPut() ;


	Tbin = getNum( num[(tag+1)%2] , bin_) ;
	

	 b = getBin(num[tag%2]) ;


	for ( int i = b+1 ; i <=100 ; i++ )
	{
		if ( Tbin == getNum(num[tag%2], i ))
		{
			x = i ;
			break ;
		}
	}

	if ( x == -1 )
		printf("Impossible") ;
	else
	{
		printf("%d", x ) ;
	}
//	system("pause") ;
	return 0 ;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值