1010. Radix (25)-pat

Source: Internet
Author: User
1010. Radix (25) Time Limit 400 ms memory limit 32000 kb code length limit 16000 B discriminant program standard author 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
Recommendation index :※※※
Source: http://pat.zju.edu.cn/contests/pat-a-practise/1010
The general meaning of this question is that there are two numbers, and the hexadecimal number of one of them is known. Assuming that the two numbers are equal, the hexadecimal number of the other is obtained. If so, the hexadecimal number is output, if not, output impossible.
Hexadecimal conversion is a common code. First of all, I thought of trying each number starting from 1.
Think about it again. This is actually to find a proper hexadecimal system so that an input is equal to a specific number. A specific hexadecimal number may be larger than or smaller than a specific number. This is a bit like a binary method. In fact, we actually use a binary method.
The following code is attached:
# Include <iostream> # include <math. h> # define N 11 using namespace STD; int chartoint (char ch) {// convert a char to a number if (CH> = '0' & Ch <= '9') {return ch-'0 ';} else if (CH> = 'A' & Ch <= 'Z') {return ch-'A' + 10;} return-1 ;} int find_radix (INT N1 [], int N2 [], int radix1, int Len) // N1: number of notified hexadecimal, N2: number of unknown hexadecimal, Len: n2 number length {long p, q; long K, I, max_k, min_k; P = 0; I = 0; while (N1 [I]! =-1) {// obtain the number p = N1 [I ++] + p * radix1;} If (LEN = 1) {If (N2 [0] = P) return N2 [0] + 1; elsereturn-1;} else {max_k = POW (p, 1.0/(len-1 )); // upper limit of hexadecimal min_k = POW (p, 1.0/Len); // lower limit of hexadecimal K = 0; do {// K = (min_k + max_k) /2; q = 0; I = 0; while (N2 [I]! =-1) {q = n2 [I] + Q * k; I ++;} If (q> P) max_k = K-1; else if (q <p) min_k = k + 1; elsereturn K;} while (min_k <= max_k);} return-1;} int main (void) {int N1 [N], n2 [N]; int tag, radix_know, radix_find; int I, j; int COUNT = 0; int len_n1 = 0, len_n2 = 0; char ch; for (I = 0; I <n; I ++) {N1 [I] = 0; N2 [I] = 0;} CH = getchar (); I = 0; while (Ch! = '') {N1 [I ++] = chartoint (CH); CH = getchar (); len_n1 ++;} N1 [I] =-1; ch = getchar (); I = 0; while (Ch! = '') {N2 [I ++] = chartoint (CH); CH = getchar (); len_n2 ++;} N2 [I] =-1; cin> tag; CIN> radix_know; If (TAG = 1) radix_find = find_radix (N1, N2, radix_know, len_n2); elseradix_find = find_radix (N2, N1, radix_know, len_n1); If (radix_find =-1) cout <"impossible" <Endl; elsecout <radix_find <Endl; return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.