Numeric conversion problem (a) of adjacent base geometric figures: determining ary

Numeric conversion problem (a)

Adjacent base geometric figures: OK band

* 6 * 9 = 42 for decimal is wrong, but for the band 13 is correct. I.e., 6 (13) 9 * (13) = 42 (13), and 42 (13) = 4 * 131 + 2 * 54 = 130 (10). Your job is to write a program reads three integers p, q and r, and then determines a band B (2 <= B <= 16) such that p * q = r. If there are many choices B, a minimum output . For example: p = 11, q = 11 , r = 121. is 11 (3) 11 * (3) = 121 (3) Since 11 (3) * 31 = 1 * 30 + 1 = 4 (10) and 121 (3) = 1 * 32 + 2 31 + 1 * 30 = 16 (10). For 10-ary, 11 (10) * 11 (10) = 121 (10). In this case, 3 should be output. If no suitable notation, and then outputs 0.
Input data
is input group T test samples. T is given in the first row. Each set of test samples per line, comprising three integers p, q, r. p, q, r are numbers of all the bits, and 1 ≤ p, q, r ≤ 1,000,000.
Output request
line output for each test sample. The row contains an integer: even have to p * q = r established minimum B. If no suitable B, the output 0.

Code
#include <stdio.h>
#include <string.h>
// X value array of characters long b2ten (char * x, int b), b -ary parameter function is converted to a decimal number hexadecimal b
{
int RET = 0;
int len = strlen (X); // get the string length
for (int I = 0; I <len; I ++)
{
iF (X [I] - '0'> = B) determines whether the ratio of // b large
return -1;
RET = b;
RET = X + [I] -'0 ';
}
return (Long) RET;
}
int main (int argc, char
the argv [])
{
int n-;
char P [. 8] , Q [. 8], R & lt [. 8];
Long pAlgorism, qAlgorism, rAlgorism;
int B;
Scanf ( "% D", & n-);
the while (N-)
{
Scanf ( "% S% S% S", P, Q, R & lt);
for (B = 2; B <= 16; B ++)
{
pAlgorism b2ten = (P, B);
qAlgorism = b2ten(q,b);
rAlgorism = b2ten(r,b);
if( pAlgorism == -1 || qAlgorism == -1 || rAlgorism == -1)continue;
if( pAlgorism * qAlgorism == rAlgorism)
{
printf("%d\n",b);
break;
}
}
if( b == 17)printf(“0\n”);
}
return 0;
}
Problem-solving ideas
(1) Select a hexadecimal B, according to the binary multiplicand, multiplier, the product converted to decimal, respectively. Then judge holds. This equation is valid so that the minimum B is the desired result.
(2) a character array stored respectively p, q, r symbols of the digits. In a first reading of the character string of p, q, r, then a different binary converting them to a decimal number, it is determined whether or not equal.
to sum up
(1) The most problem-solving ideas beginning to be understood, but like a long time did not understand the code, so I came to help understand by search words.
RET : subroutine return instruction; pc exit from the stack is higher 8 bits and lower 8-bit bytes, the stack pointer minus 2, program execution continues from the value pc. It does not affect any flags.
Stack: a data structure of a data item in sequence, only one end (referred to as a top of the stack (Top)) to insert and delete data items.
strlen: strlen () is a function, is the work of a counter, which from a certain location in memory (can be a beginning of the string, in the middle somewhere, and even some unspecified area of memory) start scanning until he came to the first string terminator '\ 0' so far, and then returns the counter value (length does not include '\ 0').
strlen and the sizeof: strlen () function to calculate the actual length of the string is encountered first '\ 0' end. Sizeof returns the amount of memory occupied by the variable declaration, not the actual length, in addition sizeof function not only is an operator, strlen () is a function.
len: len function is the function returns the number of characters in a text string. Len (): get the length of the string. The syntax is Len (string), string type variable return value Long.
Algorism: decimal notation, the code name only facilitate understanding.
(2) a single character into a digital method is int (char [i] - ' 0');

Guess you like

Origin blog.csdn.net/m0_45128748/article/details/91074550