[Project Euler]: Question 008
Zhou yinhui
Problem description:
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Problem Analysis
The above lists the 1000 consecutive numbers (although divided into 10 books, the end of line N and the beginning of line n + 1 are also consecutive ), find the product of 5 consecutive numbers.
This question is relatively simple. The basic idea is to traverse, but you can find a way to remove unnecessary operations,
<title></title>
Typical unnecessary operations include:
1. One of the five digits is 0;
2. abcdef indicates a digital string. When we move forward from ABCDE to bcdef, if F is not greater than a, the forward product of the latter is certainly not larger than that of the former, so we can avoid calculating the forward product of bcdef, it reduces the number of operations for finding five consecutive connections from 995 to 318.
ReferenceCode:
# Include <stdio. h> int test (char * Str ){ Int max = 0, temp, I; Char * Start = STR, * end; For (START = STR; * (END = (start + 5 ))! = '\ 0'; Start ++) { // If 0 is encountered, the computation near 0 should be skipped. // Skip the five digits. Because start ++ must add 1, add 4 here. If (* end = '0 ') { Start + = 4; Continue; } // If you think of the current five numbers as a queue, // Each step forward adds a number at the end and discards the number in the header. // The four numbers in the middle are the same, so the added number and the discarded number are compared. // If the number to be added is not greater than the number to be discarded, it is unnecessary to calculate the number. If (Start> STR & * end <= * (START-1 )) { Continue; } Temp = 1; For (I = 0; I <5; I ++) { Temp * = (* (start + I)-48 ); } If (temp> MAX) { Max = temp; } } Return Max;} int main (){ Char * numstr = "73167176531330624919225119674426574742355349194934 \ 96983520312774506326239578318016984801869478851843 \ 85861560789112949495459501737958331952853208805511 \ 12540698747158523863050715693290963295227443043557 \ 66896648950445244523161731856403098711121722383113 \ 62229893423380308135336276614282806444486645238749 \ 30358907296290491560440772390713810515859307960866 \ 70172427121883998797908792274921901699720888093776 \ 65727333001053367881220235421809751254540594752243 \ 52584907711670556013604839586446706324415722155397 \ 53697817977846174064955149290862569321978468622482 \ 83972241375657056057490261407972968652414535100474 \ 82166370484403199890008895243450658541227588666881 \ 16427171479924442928230863465674813919123162824586 \ 17866458359124566529476545682848912883142607690042 \ 24219022671055626321111109370544217506941658960408 \ 07198403850962455444362981230987879927244284909188 \ 84580156166097919133875499200524063689912560717606 \ 05886116467109405077541002256983155200055935729725 \ 71636269561882670428252483600823257530420752963450 \ "; Printf ("% d \ n", test (numstr )); Return 0 ;}