Question:
A piece of C # code is provided, requiring you to understand the program and answer questions without using a computer.
Answer:
The following is the c ++ code rewritten from C # code: </P> <p> # include "stdafx. H "<br/> # include <iostream> <br/> # include <stdio. h> <br/> # include <time. h> <br/> # include <math. h> <br/> # include <stdlib. h> <br/> # include <algorithm> <br/> # include <queue> <br/> # include <windows. h> <br/> using namespace STD; </P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/> {<br/> int RG [] = <br/> {<br/> 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, <br/> 12, 13, 14, 15, 16, 17, 1 8, 19, 20, 21, <br/> 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 <br/> }; </P> <p> for (_ int64 I = 1; I <_ i64_max; I ++) <br/>{< br/> int hit = 0; <br/> int hit1 =-1; <br/> int hit2 =-1; <br/> for (Int J = 0; j <sizeof (RG) /sizeof (RG [0]) & hit <= 2; j ++) <br/> {<br/> If (I % RG [J])! = 0) <br/>{< br/> hit ++; <br/> If (hit = 1) <br/>{< br/> hit1 = J; <br/>}< br/> else if (hit = 2) <br/>{< br/> hit2 = J; <br/>}< br/> else <br/>{< br/> break; <br/>}< br/> If (hit = 2) & (hit1 + 1 = hit2 )) <br/>{< br/> printf ("found % i64d/N", I ); <br/>}</P> <p> system ("pause"); <br/> return 0; <br/>}</P> <p>
Answer:
To understand this program, we can trace it from the output point. The output condition of this program is hit = 2 & (hit1 + 1 = hit2). Let's look back at what the three variables hit, hit1, and hit2 represent, hit indicates that I % R [J] is satisfied. The number of times the condition is equal to 0. Hit = 2 indicates that the condition can only be met twice. That is to say, for an I, in the 30 count of the RG array, this I can be divisible by 28 other numbers, but not by two of them. Hit1 indicates the first subscript that cannot fully divide the number of I. hit2 indicates the second subscript that cannot fully divide I. The difference between the two subscripts is only one.
Therefore, the program is looking for such a number: This number I cannot be divided by two adjacent numbers in the 30 numbers of 2-31, but can be divisible by the other 28. Therefore, this I must be an integer multiple of the smallest public multiples of the other 28 numbers. However, I cannot be divisible by two adjacent numbers, so it must be a matter factor that is not included in the quality factor of the two numbers after the resolution factor, or the number of times the I prime factor is less than the number of times the two numbers share the same prime factor.
Then, we only need to break down the prime factor for the 30 numbers from 2 to 31 to find out whether there are such two adjacent numbers, or their quality factors have quality factors not available for other numbers, either for the same prime factor, these two numbers contain this prime factor more times than all others (note that only the number of times the prime factor is higher than the number of other RG numbers, instead of the sum of the number of all other times of this factor, the reason is that the minimum public factor is needed ). Create a table as follows:
As shown in the preceding table, only the numbers 16, 17, 19, 23, 25, 27, 29, and 31 contain the most frequently used quality factors. The adjacent values are only 16 and 17. Therefore, the number I required by this program is that it cannot be divisible by 16 or 17, but it can be divisible by the other 28 in 30. The smallest I is the least common multiple of the other 28, as shown in the preceding table, the smallest I is: 2 ^ 3*3 ^ 3*5 ^ 2*7*11*13*19*23*29*31, the number calculated by the calculator is 2123581660200. We can initialize the I in the for loop in the above program into this number to test.