8617 factorial numbers and

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.