zoukankan      html  css  js  c++  java
  • pat 1010 Radix

    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 tagis 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

    Problem Solving:
    The min radix must bigger than the minimun num in the num that need to caculate its radix, and the max radix must no more than value of the num which has been given radix.
    The way to find unknown radix is dichotomy , cause if use exhaustive method, one case will out of time. And you also need to consider if the value of num is out of range.
    Here the source of this idea:https://blog.csdn.net/wyg1997/article/details/75513946

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    typedef long long LL;
    LL toNum(char c)
    {
       if (c >= '0' && c <= '9')
          return c - '0';
       return c - 'a' + 10;
    }
    LL toDecimal(string str, LL radix)
    {
       LL num = 0;
       for (int i = 0; i < str.length(); i++)
       {
          num = num * radix + toNum(str[i]);
          if (num < 0)
             return -1;
       }
       return num;
    }
    int main()
    {
       string a, b;
       int tag, radix;
       LL l, r, mid;
       cin >> a >> b >> tag >> radix;
       if (tag == 2)
       {
          swap(a, b);
       }
       LL base = toDecimal(a, radix);
       l = 2;
       r = base;
       for (int i = 0; i < b.size(); i++)
       {
          l = max(l, toNum(b[i] + 1));
       }
       while (r >= l)
       {
          mid = (l + r) >> 1;
          LL t = toDecimal(b, mid);
          if (t >= base || t == -1)
             r = mid - 1;
          else
             l = mid + 1;
       }
       if (toDecimal(b, l) == base)
          cout << l;
       else
          puts("Impossible");
       return 0;
    }

    Impression:

    I really appreciate the solve of this problem, it has a really clear thinking, and functional separation is also very good. I'll learn from the author.

  • 相关阅读:
    [你必须知道的异步编程]C# 5.0 新特性——Async和Await使异步编程更简单
    [C# 开发技巧系列]使用C#操作Word和Excel程序
    全面解析C#中参数传递
    VSTO之旅系列(四):创建Word解决方案
    [C# 开发技巧系列] 使用C#操作幻灯片
    VSTO之旅系列(五):创建Outlook解决方案
    [C# 开发技巧系列]C#如何实现图片查看器
    [你必须知道的异步编程]——异步编程模型(APM)
    [你必须知道的异步编程]——基于任务的异步模式
    [C# 开发技巧系列]如何动态设置屏幕分辨率
  • 原文地址:https://www.cnblogs.com/stormax/p/11070630.html
Copyright © 2011-2022 走看看