PAT 1010. Radix (25)(进制转化)(暂无)

来源:互联网 发布:淘宝天猫流量互刷软件 编辑:程序博客网 时间:2024/06/05 18:03

题目

1010. Radix (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.好像也是这样

自己错误代码

#include<string>#include<iostream>#include<math.h>using namespace std;int main(int argc, char *argv[]){    string a,b; int tag,radix;    cin >> a >> b >> tag >> radix;    string c;    //把直到的放到前面,然后求第二个数即可    if (tag==2) {        c = a;        a = b;        b = c;    }    //求出a的十进制数是多少    int len_a = a.length();    int a_ten = 0;    for (int i = 0; i < len_a; ++i) {        a_ten += (a[i] - '0')* pow(radix,len_a - i - 1);    }    //cout << a_ten;    //从b的1进制开始,如果小域a,则增加进制,如果大于则输出不能,等于则找到了,即输出即可    int radix_b = 1,tem_b = 0;    int len_b = b.length();    while (1) {        //重置tem_b        tem_b = 0;        //把b(radix_b进制)也转化为十进制,然后根据上述方式做判断        for (int i = 0; i < len_b; ++i) {            tem_b += (b[i] - '0') * pow(radix_b,len_b - i - 1);        }        if (tem_b == a_ten) {            cout << radix_b << endl;            break;        }        if (tem_b > a_ten) {            cout << "Impossible" <<endl;            break;        }        radix_b++;    }    return 0;}
0 0
原创粉丝点击