当前位置:编程学习 > C/C++ >>

PAT-1010. Radix (25)

题目描述:
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可以是非常大的数。
最初的代码如下:
[cpp] view plaincopy
#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不够大的缘故。
 
 
#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
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,