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 

刚开始以为此题的范围会超过long long int,试过后发现并没有。另外题目说字符串里面的数字只有{0-9,a-z},让我以为radix的范围不会超过36,后来发现错了。更改后提交,发现有一个case超时了,估计这个case里的radix非常大,于是radix也采用long long int,并且采用二分搜索,这才解决超时问题。注意这VS下long long int应该定义成__int64,输入输出格式为%I64d。

代码

  1 #define __int64 long long
  2 #include <stdio.h>
  3 #include <string.h>
  4 
  5 long long calculateFunc(char *,long long);
  6 int maxDigit(char *);
  7 int char2num(char);
  8 int main()
  9 {
 10     long long N1,N2,radix;
 11     int tag;
 12     char str1[],str2[];
 13     char *p1,*p2;
 14     while(scanf("%s %s",str1,str2) != EOF){
 15         scanf("%d%I64d",&tag,&radix);
 16         if(tag == ){
 17             p1 = str1;
 18             p2 = str2;
 19         }
 20         else{
 21             p1 = str2;
 22             p2 = str1;
 23         }
 24         N1 = calculateFunc(p1,radix);
 25         int radixMin = maxDigit(p2) + ;
 26         if (radixMin < )
 27             radixMin = ;
 28         int strN = strlen(p2);
 29         if(strN == ){
 30             N2 = char2num(*p2);
 31             if(N2 == N1){
 32                 printf("%I64d\n",N2+);
 33             }
 34             else{
 35                 printf("Impossible\n");
 36             }
 37             continue;
 38         }
 39         long long i = radixMin;
 40         N2 = calculateFunc(p2,i);
 41         int flag = ;
 42         while(N2 <= N1){
 43             if(N1 == N2){
 44                 printf("%I64d\n",i);
 45                 flag = ;
 46                 break;
 47             }
 48             i *= ;
 49             N2 = calculateFunc(p2,i);
 50         }
 51         if(!flag){
 52             long long s = i / ,e = i;
 53             long long mid;
 54               while(e >= s){
 55                 mid = (s + e) / ;
 56                 N2 = calculateFunc(p2,mid);
 57                 if(N1 == N2){
 58                     printf("%I64d\n",mid);
 59                     flag = ;
 60                     break;
 61                 }
 62                 else if (N1 > N2){
 63                     s = mid + ;
 64                 }
 65                 else{
 66                     e = mid - ;
 67                 }
 68               }
 69             if(!flag){
 70                 printf("Impossible\n");
 71             }
 72         }
 73     }
 74     return ;
 75 }
 76 
 77 long long calculateFunc(char *p,long long radix)
 78 {
 79     int n = strlen(p);
 80     int i;
 81     long long N = ;
 82     for(i=;i<n;++i){
 83         N = N * radix + char2num(*(p+i));
 84     }
 85     return N;
 86 }
 87 
 88 int maxDigit(char *p)
 89 {
 90     int n = strlen(p);
 91     int i;
 92     int m = -;
 93     int t;
 94     for(i=;i<n;++i){
 95         t = char2num(*(p+i));
 96         if (t > m)
 97             m = t;
 98     }
 99     return m;
 }
 
 int char2num(char c)
 {
     if(c >= '' && c <= '')
         return c - '';
     else if(c >= 'a' && c <= 'z')
         return c - 'a' + ;
     else
         return -;
 }

PAT 1010的更多相关文章

  1. PAT 1010 一元多项式求导 (25)(STL-map+思路)

    1010 一元多项式求导 (25)(25 分)提问 设计函数求一元多项式的导数.(注:x^n^(n为整数)的一阶导数为n*x^n-1^.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均 ...

  2. 已经菜到不行了 PAT 1010. Radix (25)

    https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...

  3. PAT 1010. 一元多项式求导

    1010. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数 ...

  4. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

  5. PAT 1010. 一元多项式求导 (25)

    设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与 ...

  6. PAT——1010. 一元多项式求导

    设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与 ...

  7. PAT 1010 Radix 进制转换+二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  8. PAT 1010 Radix (二分)

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  9. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

随机推荐

  1. LeetCode Same Tree (判断相同树)

    题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...

  2. Logminer实战

    相信许多DBA同学都曾遇到过这种情况,由于各种各样的原因,必须对数据库做不完全恢复:但又不确定应该恢复到哪个时间点或SCN才合适,因而反复的执行不完全恢复,悲剧掉了一地,中枪的有木有?温馨提示下,在生 ...

  3. 应用MVP模式写出可维护的优美Android应用

    在Android开发中,我们常常会动辄写出数千行的Java类,而当一个Activity有4.5千行的时候,想找一个逻辑在哪儿就会显得异常痛苦了.比如想在数据加载错误的时候,显示一个提示信息,上上下下得 ...

  4. 安装ejabberd2并配置MySQL为其数据库

    以前用过openfire做为服务器,但是openfire的集群支持不是很好,所以改用Ejabberd,由于它是用Erlang语言开发的,其并发率与分布式的功能都是很强悍的,在此我记录一下我的安装与配置 ...

  5. Ext入门学习系列(四)面板控件

    第四章 使用面板 上节学习了Ext复杂对话框,更进一步了解了Ext的运行机制.本章重点来了解Ext所有控件的基础——面板控件. 一.Ext的面板是什么? 同样先来看看几个效果: 基本面板,点击右上角小 ...

  6. [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例

    本文我们来学习一下在Entity Framework中使用Context删除多对多关系的实体是如何来实现的.我们将以一个具体的控制台小实例来了解和学习整个实现Entity Framework 多对多关 ...

  7. vs2010调用matlab2011下的.m文件

    很幸运在网上找到了采用引擎的方法,用vs2009调用matlab2008下的.m文件:但个人的环境是vs2010+matlab2011;想着二者差不多,故将s2010调用matlab2008拿来试试: ...

  8. 转载:C++ 虚函数表解析

    目录(?)[+]   转载:http://blog.csdn.net/haoel/article/details/1948051# 前言 C++中 的虚函数的作用主要是实现了多态的机制.关于多态,简而 ...

  9. WebService的发布及客户端的调用

    一.目录 1.JAX-WS发布WebService 1.1 创建一个简单的WS 1.2 打包部署和发布 2.CXF+Spring发布WebService 3.客户端的调用方式 二.正文 1. JAX- ...

  10. 第一章:绪论-Python开发工具的安装

    书中提到了操作系统平台尽量选 *nix.我这里选用的是 ubuntu 14.04 , 下面的操作均以此操作系统为例说明. 操作系统安装教程可以去网站上找,推荐用虚拟机的方式,Windows下可用的虚拟 ...