Title Description
Design an algorithm that calculates the number of trailing zeros for the factorial of N.
Given an int n, return the number of the 0 tails of the factorial of N. Guarantees that n is a positive integer.
Test examples:
5
Returns: 1
#include <iostream>using namespace STD;intGrial (intx) {inttemp = x;intCount2 =0;intCOUNT5 =0; while(temp) {Count2 + = temp/2; Temp/=2;//Here is a focus, such as seeking n! in the number of 2, count=n/2+n/(2*2) ..... //For example, to find the number of n! 3. / * int count = 0; while (n) {COUNT+=N/3; n/=3;//is so simple. } */} temp = x; while(temp) {COUNT5 + = temp/5; Temp/=5; }//n! in the number of 0 is nothing more than to find the number of 2,5, the number of the smallest of the number. returnCount2 > COUNT5? Count5:count2;}intMain () {cout<<grial ( +) <<endl;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithm title: Factorial tail 0