Time limit : 500MS Memory limit : 1000K commits: 224 number of passes :
Question types : programming language : C + +; C
Description
We know the factorial of a number n! = 1*2*......*n is generally a large number. Now we're going to calculate the sum of the numbers in all the digits that come after the number factorial.
Input format
Enter as multi -case input. only one row per case, enter a positive integer n (1<=n <=), end when input 0
Output format
An integer.
Input sample
5
2
0
Output sample
3
/*this simulates the multiplication of large numbers to calculate the high-precision factorial*/#include<stdio.h>#include<string.h>#defineMAX 100intMain () {intF[max];/*Larger factorial can be computed by modifying the size of the array*/ inti,j,s,k,n,sum; while(1) {memset (F,0,sizeof(f)); f[0]=1;/*this has to be*/sum=0; scanf ("%d",&N); if(n==0) Break; for(i=2; i<=n; i++) {k=0;/*K is rounding*/ for(j=0; j<max; J + +)//like calculating 5! Initialization Results 1 0 0 0 0 0 0 0 .... {//2 times the above numbers, 2 0 0 0 0 0 0 0 .... S=i*f[j]+k;//3 times the above numbers, 6 0 0 0 0 0 0 0 .... f[j]=s%Ten;//4 times the above numbers, 4 2 0 0 0 0 0 0 .... k=s/Ten;//5 times the above numbers, 0 2 1 0 0 0 0 0 .... } } for(i=max-1; i>=0; i--)if(F[i]) Break;/*due to reverse storage results, the excess 0 should be removed from the back*/ for(J=i; j>=0; j--) sum+=F[j]; printf ("%d\n", sum); }}/*Obviously, this algorithm is very not efficient, it should be for many times the Shenyu operation*/
View Code
2
Time:15ms
8617 factorial numbers and