zoukankan      html  css  js  c++  java
  • 1010 Radix (25 分)

    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

    题目分析:将两个元素转换为10进制进行比较 ,对于那个不确定进制的参数,我们可以使用二分法来求它的进制
    参考了柳神的答案
     1 #include<iostream>
     2 #include<string>
     3 #include<stdlib.h>
     4 #include<vector>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 long long tolong(string str, long long radix)
     9 {
    10     long long length = str.length();
    11     long long num = 0;
    12     for (long long j = 0; j < length; j++)
    13         num = (isdigit(str[j])) ? num * radix + str[j] - '0' : num * radix + str[j] - 'a' + 10;
    14     return num;
    15 }
    16 long long Find_radix(string num,long long N)
    17 {
    18     char it = *max_element(num.begin(), num.end());
    19     long long low = ((isdigit(it)) ? it - '0' : it - 'a' + 10)+1;
    20     long long high = max(N,low);
    21     long long mid;
    22     while (low<=high)
    23     {
    24         mid = (low + high) / 2;
    25         long long n = tolong(num, mid);
    26         if (n > N||n<0)
    27             high = mid-1;
    28         else if (n < N)
    29             low = mid+1;
    30         else
    31             return mid;
    32     }
    33     return -1;
    34 }
    35 
    36 int main()
    37 {
    38     string N1, N2;
    39     long long tag, radix;
    40     cin >> N1 >> N2 >> tag >> radix;
    41     long long flag = 0;
    42     flag = tag == 1 ? Find_radix(N2, tolong(N1, radix)) : Find_radix(N1, tolong(N2, radix));
    43     if (flag==-1)
    44         cout << "Impossible";
    45     else
    46         cout << flag;
    47     return 0;
    48 }
    View Code
     
  • 相关阅读:
    建立连接数据库时要输入哪些信息?
    如何建立一个JDBC程序?
    如何在网上找MySQL数据库的JDBC驱动jar包?
    gorm连接mysql数据库
    Django ORM的骚操作
    Python 发送企业微信单发和群发机器人
    python获取指定间隔日期列表
    表名小写_set.all()再根据字段过滤
    foreignkey相关的参数
    Django 模型层-多表操作
  • 原文地址:https://www.cnblogs.com/57one/p/11867020.html
Copyright © 2011-2022 走看看