设为首页 加入收藏

TOP

1010. Radix (25)
2014-11-23 21:38:19 来源: 作者: 【 】 浏览:9
Tags:1010. Radix
题目描述:
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
#include
#include
using namespace std;
#define max 10
int change(char *s, int base)
{
	int temp = 0;
	int i;
	for(i=0; 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< 
  
#include 
#include 

#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> 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;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++类和对象的一个简单的实例 下一篇POJ 1659 Frogs' Neighborhood

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)