【PAT】1010. Radix (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1010

题目描述:

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

分析:

(1)一开始以为对进制进行遍历就行,但是没有考虑到起始radix可以是非常大的数。

最初的代码如下:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
#define max 10
int change(char *s, int base)
{
	int temp = 0;
	int i;
	for(i=0; i<strlen(s); i++)
	{
		if(s[i]>='0' && s[i]<='9')
		{
			if((s[i] - '0') >= base) return -1;
			temp = temp*base + (s[i] - '0');
		}			
		else
		{
			if( (s[i]-'a'+10) >= base ) return -1;
			temp = temp*base + (s[i] - 'a' + 10);
		}
	}
	return temp;
}

int main( )
{
	char N1[max];
	char N2[max];
	int tag;
	long long result1,result2,radix;
	long long i;
	bool flag = false;
	scanf("%s%s%d%ld",N1,N2,&tag,&radix);
	if(tag == 1)
	{
		result1 = change(N1,radix);		
		for(i=2; i<=10000000; i++)
		{
			result2 = change(N2,i);			
			if(result2 == result1) {flag = true; break;}
		}		
	}
	else if(tag == 2)
	{
		result2 = change(N2,radix);
		for(i=2; i<=10000000; i++)
		{
			result1 = change(N1,i);
			if(result1 == result2) {flag = true; break;}
		}
	}
	if(flag)
		cout<<i<<endl;
	else
		cout<<"Impossible"<<endl;
	return 0;
}

运行后结果有两组“答案错误”,估计是radix不够大的缘故。



(2)参考:http://blog.csdn.net/wzb56_earl/article/details/7330762

附上 wzb56 的代码:

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

#define max 11

char a[4][max];

long long num2Dec(char * p, long long radix)
 {
      long long  i;
      long long  len=strlen(p);

      long long digit = 0;
      long long m = 1;
      long long sum = 0;
      for(i=len-1;i>=0;i--)
      {
         if(p[i]>='a'&&p[i]<='z')
              digit= p[i] - 'a' + 10;
         else if(p[i]>='0'&& p[i]<='9')
              digit=p[i] - '0';
          sum+=digit*m;
          m*=radix;
      }
      return sum;
 }

 int findLeastRadix(char *p)
  {
     long long  len=strlen(p);
     long long  low=0;
     long long num;
     long long i;
     for(i=len-1;i>=0;i--)
      {
          if(p[i]>='a'&&p[i]<='z')
             num= p[i] - 'a' + 10;
          else if(p[i]>='0'&& p[i]<='9')
              num=p[i] - '0';
          if(num+1>low)
              low=num+1;
      }
      return low;

 }

int compare(char* p, long long radix, long long target)
 {
     long long  i;
     long long len=strlen(p);

     long long m = 1;
     long long num = 1;
     long long sum = 0;
     for(i=len-1;i>=0;i--)
     {
         if(p[i]>='a'&&p[i]<='z')
             num= p[i] - 'a' + 10;
         else if(p[i]>='0'&& p[i]<='9')
             num=p[i] - '0';
         sum+=num*m;
         m*=radix;
         if(sum>target)  //avoid  overflow
             return 1;
     }

     if(sum>target)
         return 1;
     else if(sum<target)
         return -1;
     else
         return 0;

 }


long long binarySearch(char* p, long long low, long long high, long long target){

    long long mid = low;
    int tag;

    while(low<=high) {

        tag = compare(p, mid, target);

        switch(tag) {
            case -1:
                low = mid + 1;
                break;
            case 0:
                return mid;
            case 1:
                high = mid - 1;
        }

        mid = (low + high) >> 1;
    }

    return -1;
}


int main(int argc, char* argv[])
{
    int i;
    int tag;
    int base;
    long long n1;
    long long n2;
    long long low;
    long long high;
    long long radix;

    for(i=0; i<4; i++) {
        scanf("%s", a[i]);
    }

    tag = atoi(a[2]);
    base = atoi(a[3]);

    switch(tag) {
        case 1 :
            n1 = num2Dec(a[0], base);
            low=findLeastRadix(a[1]);
            high = (n1+1 > low+1) ? n1+1 : low+1;
            radix = binarySearch(a[1], low, high, n1);
            break;

        case 2 :
            n2 = num2Dec(a[1], base);
            low=findLeastRadix(a[0]);
            high = (n2+1 > low+1) ? n2+1 : low+1;
            radix = binarySearch(a[0], low, high, n2);
            break;
    }

    if(-1 != radix){
         printf("%ld\n", (long)radix);
    }
    else {
        printf("Impossible");
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值