工学1号馆

home

Project Euler 20–Factorial digit sum

Wu Yudong    August 12, 2018     欧拉计划   527   

n! means n × (n − 1) × … × 3 × 2 × 1

For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

题目大意:

n! = n × (n − 1) × … × 3 × 2 × 1

例如,10! = 10 × 9 × … × 3 × 2 × 1 = 3628800, 那么10!的各位之和就是3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

算出100!的各位之和。

#include <stdio.h>
#include <math.h>
#define N 100

int main(void)
{
	int n = N * log(N / 3), a[n], ca = 0, i, j;
	for (i = 0; i < n; i++)
		a[i] = 0;
	a[n - 1] = 1;
	for (i = 1; i <= N; i++) {
		for (j = n - 1; j >= 0; j--) {
			ca = i * a[j] + ca;
			a[j] = ca % 10;
			ca /= 10;
		}
		ca = 0;
	}
	ca = 0;
	for (i = 0; i < n; i++)
		ca += a[i];
	printf("%d\n", ca);
	return 0;
}

Answer:648
Completed on Sun, 13 Apr 2014, 02:47

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)