【c++】PAT (Advanced Level)1010. Radix (25)

来源:互联网 发布:上古卷轴5enb优化 编辑:程序博客网 时间:2024/04/30 12:03


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.只有一位数的时候,取最小啊。2 18/25测试点还有几个

2。第二遍写 代码清晰了一点,但是还是没找出来测试点~

3.基数是可能超过35的 

无聊给福利:

radix 测试点 
 
答案为10          1 13 8                  4分
答案为Impossible  14 17 18 2    7分
答案为 35         15                            2分
答案为 2          6                              2分

超过35 的        11 3 4 5                4分                               

#include <iostream>#include <iomanip>#include <string>using namespace std;int thenum(char a1){int cc;if(a1<='9'&&a1>='0'){cc=a1-'0';}else{cc=a1-'a'+10;}return cc;}int thesum(string a1,int radix){int n=a1.size();    int rr=1,b2=0;int b1=0;while(n--){b1=thenum(a1[n])*rr+b1;rr=rr*radix;}return b1;}int maxa(string a){int n=a.size(),max=0;for(int i=0;i<n;i++){int temp=thenum(a[i]);if(temp>max){max=temp;}}max++;if(max<2){max=2;}return max;}int main() {//freopen("in.txt","r",stdin);string N1,N2,a1,a2;int tag,radix;cin>>N1>>N2>>tag>>radix;if(tag==1){a1=N1,a2=N2;}else {a1=N2,a2=N1;}int b1,i;b1=thesum(a1,radix);int mm=maxa(a2);for(i=mm;i<=70;i++){if(i==radix)continue;if(b1==thesum(a2,i)){cout<<i;break;}}if(i==71){cout<<"Impossible";}system("pause");return 0;}


0 0
原创粉丝点击