美文网首页
【PAT-甲级-C++】1010. Radix (25)

【PAT-甲级-C++】1010. Radix (25)

作者: linghugoogle | 来源:发表于2018-01-23 16:19 被阅读22次

1010. Radix (25)

链接:https://www.patest.cn/contests/pat-a-practise/1010

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

1、题目

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

2、解析

1)找了半天,不知道哪里错了

3、代码

#include<iostream>
#include<string>
#include<math.h>
#define N 2002
using namespace std;
//将src进制的数字转化为10进制
long long int change(string s, int src) {
    long long int num = 0;
    int i, j, k;
    long long int pow=1;
    k = s.size();
    for (i = k-1;i >=0;--i) {
        if (s[i] >= '0'&&s[i] <= '9') 
            j = s[i] - '0';
        else
            j = s[i] - 'a'+10;
        num += j * pow;
        pow *= src;
    }
    return num;
}
int main() {
    int i, j, k1,k2;
    string s1, s2,tmp;          
    int tag, rad,rad2;
    long long int value1, value2;       //之后可能越界,改为long 
    //读取数据
    cin >> s1 >> s2 >> tag >> rad;

    //数据预处理
    if (tag == 2) {
        tmp = s1;
        s1 = s2;
        s2 = tmp;
    }
    k1 = s1.size();
    k2 = s2.size();

    //n1转化为10进制
    value1 = change(s1, rad);

    //判断n2的最小进制数
    rad2 = 0;
    for (i = 0;i < k2;++i) {
        if (s2[i] >= '0'&&s2[i] <= '9')
            j = s2[i] - '0';
        else
            j = s2[i] - 'a' + 10;
        if (j > rad2)
            rad2 = j;
    }
    rad2 += 1;
    if (rad2 < 2)
        rad2 = 2;

    //直接求解有一个值一直过不了,估计是上限太小了,可以改为二分法查找
    //对s2求各种进制的10进制数值
    bool flag = false;
    for (i = rad2;i <= 99;++i) {
        //value2 = change(s2, rad2);
        //这里写错了,搞了半天,应该是
        value2 = change(s2,i);
        if (value2 == value1) {
            flag = true;
            break;
        }
    }
    /*
    //二分法查找
    long long int min=rad2,max = value1 + 1,avg;
    bool flag = false;
    while (min <= max) {
        avg = (min + max) / 2;
        value2 = change(s2, avg);
        //采用过大的进制,造成数值过大
        if (value2 > value1 || value2==-1) {
            max = avg - 1;
        }
        else if (value2 < value1) {
            min = avg + 1;
        }
        else {
            flag = true;
            break;
        }
    }*/

    //输出结果
    if (flag == true)
        cout << rad2;
    else cout << "Impossible";

    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:【PAT-甲级-C++】1010. Radix (25)

      本文链接:https://www.haomeiwen.com/subject/kswsaxtx.html