[Project Euler] 来做欧拉项目练习题吧: 题目008

                                                [Project Euler] 来做欧拉项目练习题吧: 题目008

                                                                  周银辉 

 

问题描述: 

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

 

问题分析

上面列出了连续的1000个数字(虽然分成了10行书写,但第n行的结尾和第n+1行的开头也算连续的),找出最大的连续5个数的乗积。

这个题比较简单,基本思想是遍历,但可以想办法去掉一些不必要的运算,

典型的不必要运算有

1,五个数字中,其中一个是0;

2,abcdef代表数字数字串,从abcde前进到bcdef时,如果f不大于a,则后者乗积肯定没有前者大,所以可以免去计算bcdef的乗积,其将求五个数连乗的运算从995次减少到了318次 

 

参考代码:

#include <stdio.h>
int test(char * str)
{
int max = 0, temp, i;
char *start=str, *end;
for(start = str; *(end=(start+5))!='\0'; start++)
{
//如果遇到0,则应该跳过0附近的运算
//应该跳过5个数字,因为start++还要加1,所以这里加4
if(*end == '0')
{
start += 4;
continue;
}
//如果将当前的5个数字看成一个队列的话,
//每前进一步,会在末尾添加一个数字,并舍弃头部的数字
//而中间4个数字相同,所以比较添加的数字和舍弃的数字
//如果添加的数字不大于舍弃的,那么也没必要计算了
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;
}

 

 

posted @ 2011-01-25 18:54  周银辉  阅读(2849)  评论(11编辑  收藏  举报