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!
答案:648
#include #include #include
int main(void) { char s[200] = {0}; char t[200] = {0}; char d[201] = {0}; const int len = 200;
int sum = 0; int i = 0, j = 0; int flag = 0; int m = 0, n = 0; int k = 0, ex = 0; s[0] = 2;
for (i=3; i<100; i++) { m = i % 10; n = i / 10;
if (m) { for (j=0; j { k = (flag ? ex:0) + s[j]*m; if (k<10) { t[j] = k; flag = 0; } else { t[j] = k % 10; flag = 1; ex = k / 10; } }
k = s[len-2] * m + (flag ? ex:0); if (k<10) { t[len-2] = k; flag = 0; } else { t[len-2] = k % 10; t[len-1] = k / 10; flag = 0; ex = 0; } } if (n) { for (j=0; j { k = (flag ? ex:0) + s[j]*n; if (k<10) { d[j+1] = k; flag = 0; } else { d[j+1] = k % 10; flag = 1; ex = k / 10; } }
k = s[len-2] * n + (flag ? ex:0); if (k<10) { d[len-1] = k; flag = 0; } else { d[len-1] = k % 10; d[len] = k / 10; flag = 0; ex = 0; } }
for (j=0; j { k = t[j] + d[j] + (flag ? 1:0); if (k<10) { s[j] = k; flag = 0; } else { s[j] = k%10; flag = 1; } } flag = 0; memset(t, 0, len); memset(d, 0, len); }