Title Description:
How many 0 are behind the factorial of n?
the factorial of 6 = 1*2*3*4*5*6 = 720,720 is followed by a 0. Input
A number n (1 <= n <= 10^9)
Output
Number of outputs 0
The 0 at the end of each number factorial is caused by multiplying the factor 2 and the factor 5 in the preceding factorial number, and because there are more than 5:2, just consider the number of 5.
Every 5 numbers, will produce a 0, such as 5, 10, 15 ...;
Every 25 number, will produce more than a 0, that is, two 0, such as 25/50/75 ...;
Every 125 number, will produce more than a 0, that is, three 0, such as 125, 625 ... and so on.
That is, each time the number is divided by 5, the number after the factorial is obtained.
The code is as follows:
1#include <stdio.h>2 intMain (void)3 {4UnsignedintA, B =0;5scanf"%d", &a);6 while(A! =0){ 7A = A/5;8B + =A;9 }Tenprintf"%d", b); One return 0; A}
Number of 0 after factorial