1010. Radix (25)[C语言]

xiaoxiao2021-02-28  115

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

还有一个超时。。24

#include <stdio.h> #include <stdlib.h> #include <math.h> static char cs[36]={'0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z'}; int getLength( char c[]){ int i = 0 ; for(i=0;i< 15 ; i++) { if(c[i]=='\0'){ return i; } } return -1; } int getN(char c,int rx){ int i = 0; for(i =0;i < rx ; i++){ if(cs[i] == c){ return i ; } } return -1; } long long getNum(char c[] , int rdx){ long long num = 0 ; int i = 0; for(i = getLength(c)-1; i >= 0 ;i--){ int n = getN(c[i],rdx); if(n==-1){ return -1; } num+=pow(rdx,getLength(c)-1-i)*n; } return num; } int main(){ char c1[15],c2[15]; int tag , radx ,i; scanf("%s",c1); scanf("%s",c2); scanf("%d %d" , &tag,&radx); long long num1 = 0 ; long long num2 = 0 ; if((getNum(c1,10)==0 &&getNum(c2,10)!=0) || (getNum(c1,10)!=0 &&getNum(c2,10)==0)){ printf("Impossible"); return 0 ; }else if((getNum(c1,10)==1 &&getNum(c2,10)!=1) || (getNum(c1,10)!=1 &&getNum(c2,10)==1)){ printf("Impossible"); return 0 ; } int exist = 0; if(tag==1){ num1 = getNum(c1,radx); for(i = 2 ;i >0; i ++ ){ num2 = getNum(c2,i); if(num2 < 0) { continue; } if(num1==num2){ printf("%d" , i); exist = 1; break; }else if(num2 > num1 ){ break; } } }else{ num1 = getNum(c2,radx); for(i = 2 ; i > 0; i ++ ){ num2 = getNum(c1,i); if(num2 < 0) { continue; } if(num1==num2){ printf("%d" , i); exist = 1; break; }else if(num2 > num1 ){ break; } } } if(exist==0){ printf("Impossible"); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-65165.html

最新回复(0)