Algorithm-Determining the binary algorithm
- Topic
- Describe
- Input
- Output
- Sample input
- Sample output
- Source
- Thinking of solving problems
- Key points
- Ideas
- Binary conversion
- Conversion functions
- Final implementation
Title Description
6*9 = 42 is wrong for decimal, but is correct for the 13 binary. That is, 6 (13) * 9 (13) = 42 (13), and 42 (13) = 4 * 131 + 2 * 130 = 54 (10). Your task is to write a program that reads three integers p, q, and R, and then determines that a binary B (2<=b<=16) makes p * q = r. If B has many choices, the output is the smallest one. For example: p = one, q = one, R = 121. There are 11 (3) * 11 (3) = 121 (3) because 11 (3) = 1 * 31 + 1 * 30 = 4 (10) and 121 (3) = 1 * 32 + 2 * 31 + 1 * 30 = 16 (10). For the binary 10, there are 11 (10) * 11 (10) = 121 (10). In this case, the output should be 3. If there is no appropriate input, the output is 0.
Input
The input has a T group test sample. T is given in the first line. Each set of sample samples occupies a row, containing three integers p, q, R. All bits of P, Q, R are numbers, and 1 <= p, q, R <= 1,000,000.
Output
For each test sample output one row. The line contains an integer: the smallest b that causes p * q = r to be set. If there is no appropriate B, the output
Sample input
36 9 4211 11 1212 2 2
Sample output
1330
Source
http://bailian.openjudge.cn/practice/2972
Key points of the problem solving method
All the bits of P, Q, R are numbers, and the range is [1,1000000];
2~16 in the range of the system
Ideas
Convert P,q,r to decimal to determine if P*q equals r
The pseudo code is as follows:
1. Traverse I--[2,16]
2. Convert the number of p,q,r as an I-decimal into 10-binary, set the converted number to P (i), q (i), R (i)
3. Judge P (i) *q (i) = R (i), return I when equal;
It can be seen that the difficulty of the whole problem is to convert the number of transferred into 10, this process solved, basically succeeded 90%. First, review the conversion.
Binary conversion
There is a formula: binary number, octal number, hexadecimal number of the number of each good to the respective base of the (N-1), and the sum is the corresponding decimal number. Digit, n=1; 10 bit, n=2 ... example:
110B=1X2 's 2-+1x2-square-+0x2 0-Square =0+4+2+0=6d
110Q=1X8 's 2-+1x8-square-+0x8 0-Square =64+8+0=72d
110h=1x16 's 2-+1x16-square-+0x16 0-Square =256+16+0=272d
Conversion functions
According to the formula you can determine that the input is the number n to convert, the cardinality B, and the corresponding decimal integer. In addition, if you enter a number that is greater than or equal to base on one of the digits in number, it cannot be a base binary. Therefore, you need to return a result to identify this state, which is returned here-one means
In addition, the output is a maximum of 1000000H = 1*16^6 = 2^24 < 2^31-1, so return with int, function declaration as follows.
int tranToDecimal(unsigned number,unsigned base);
The specific implementation is as follows:
/ * * b into 10 binary * A (n) a (n-1) a (n-2) ... A (1) a (0) = a (0) *b^0 + A (1) *b^1 + ... + a (n) *b^ (n) */ int trantodecimal(unsigned number,unsigned base) {intsum =0;intK =1; while(number) {//Seek subkey value intremainder = number%Ten;//cannot be greater than the binary if(Remainder >=Base) {return-1; } remainder *= k; sum + = remainder;parameters required for processing subkey valuesNumber/=Ten; K *=Base; }returnsum;}
Final implementation
With the above functions, the rest is easy to handle and the final implementation code is as follows:
int main(int argc, const Char * argv[]) {//Insert code here ... intNscanf("%d", &n); for(inti =0; I < n; i++) {unsignedPunsignedQunsignedRBOOLHasbase =false;scanf(" %d%d%d", &p,&q,&r); for(inti =2; I <= -; i++) {unsignedP10 = Trantodecimal (P, i);unsignedQ10 = Trantodecimal (q, i);unsignedR10 = Trantodecimal (R, I);if(P10! =-1&& Q10! =-1&& R10! =-1&& P10 * Q10 = = R10) {hasbase =true;printf("%d\n", i); Break; }; }if(!hasbase) {printf("0\n"); } }return 0;}
Algorithm-Determining the binary