Project Euler——Problem 20 - Daybreakcx's Blog - Keep Programming! With Algorithm! With Fun!
Project Euler——Problem 19
Project Euler——Problem 21

Project Euler——Problem 20

daybreakcx posted @ 2009年7月21日 05:06 in Prject Euler , 947 阅读

原题与答案:

Problem 20

21 June 2002

n! means n × (n - 1) × ... × 3 × 2 × 1

Find the sum of the digits in the number 100!

Answer:648

 

分析与解答:

题目的意思很明朗,是求100!的各位数字之和,啥都不说了,高精度直接计算即可,提及一下位数的估计,因为100!最坏情况下看成是100个100相乘,也就是10^{200},不超过201位,当然这还是从多计算,实际上小很多,我的代码中是顺手打了个300意思一下,程序源代码如下:

 

  1. #include<stdio.h>
  2. #include<string.h>
  3. int a[300],i,j,t,ans;
  4. int main()
  5. {
  6.         memset(a,0,sizeof(a));
  7.         a[0]=a[1]=1;
  8.         for (i=1;i<=100;i++)
  9.         {
  10.                 t=0;
  11.                 for (j=1;j<=a[0];j++)
  12.                 {
  13.                         a[j]=a[j]*i+t;
  14.                         t=a[j]/10;
  15.                         a[j]%=10;
  16.                 }
  17.                 while (t)
  18.                 {
  19.                         a[++a[0]]=t%10;
  20.                         t/=10;
  21.                 }
  22.         }
  23.         ans=0;
  24.         for (i=1;i<=a[0];i++) ans+=a[i];
  25.         printf("%d\n",ans);
  26.         return 0;
  27. }

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter