1. 程式人生 > >PTA-1010——Radix(未完全通過,通過率21/25)

PTA-1010——Radix(未完全通過,通過率21/25)

psu 最小 long long ram 出現 msu out which tst

題目:

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 N?1?? and N?2??, 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 #include<iostream>
 2 using namespace std;
 3 string n1,n2;
 4 int tag,radix;
 5 int main(){
 6     cin>>n1>>n2>>tag>>radix;
 7     long long x=0;    //此題進制轉換後可能超過int的範圍,要用long long類型 
 8     if(tag==2){
 9         string temp=n1;
10         n1=n2;
11         n2=temp;
12     }
13     
14     //將第一個數字轉為10進制
15     int t=1;;
16     for(int i=n1.length()-1;i>=0;i--){
17         if(n1[i]>=0&&n1[i]<=9){
18             x+=t*(n1[i]-0);
19         }else{
20             x+=t*(n1[i]-a+10);
21         }
22         t*=radix;
23     }
24     
25     //二分查找,否則容易超時,且題目沒有說明最大的進制是36 
26     long long l,r;    //二分查找法的上下界 
27     l=1; 
28     for(int i=0;i<n2.length();i++){        //下界的最小值 
29         if(n2[i]>=0&&n2[i]<=9){
30             if(n2[i]-0>l){
31                 l=n2[i]-0;
32             }
33         }else{
34             if(n2[i]-a+10>l){
35                 l=n2[i]-a+10;
36             }
37         } 
38     } 
39     l++;
40     if(x>l){    //如6 110,此時x=6,l=2,滿足x>l,即r=7,因為如果比7更大,那麽這個數的十進制最小也要比7大 
41         r=x+1;
42     }else{        //如3 45,此時x=3,l=6,滿足x<l,即r=7 
43         r=l+1;
44     }
45     
46     //將第二個數字轉為10進制 
47     bool flag=false;
48     long long y;
49     while(l<=r){
50         long long mid=(l+r)/2;
51         y=0;
52         t=1;
53         for(int i=n2.length()-1;i>=0;i--){
54             if(n2[i]>=0&&n2[i]<=9){
55                 y+=t*(n2[i]-0);
56             }else{
57                 y+=t*(n2[i]-a+10);
58             }
59             t*=mid;
60         }
61         if(y==x){
62             cout<<mid;
63             flag=true;
64             break;
65         }else if(y<0||y>x){        //考慮y<0是因為:可能出現轉換後溢出的情況,這時候y會小於0 
66             r=mid-1;
67         }else if(y<x){
68             l=mid+1;
69         }
70     }
71     if(!flag){
72         cout<<"Impossible";
73     }
74     return 0;
75 }

PTA-1010——Radix(未完全通過,通過率21/25)