码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-18 13:58:46      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:csdn   ++   math   less   where   base   注释   --   repr   

题目:

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)

标签:csdn   ++   math   less   where   base   注释   --   repr   

原文地址:https://www.cnblogs.com/orangecyh/p/10287205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!