static void Main(string[] args) { int n = 12; Int64 sum = 1; for (int i = 1; i <= n; i++) { sum *= i; } Console.WriteLine("结果:" + sum); Console.WriteLine(cal(n)); Console.ReadKey(); } static int cal(int n) { int temp = n; int count = 0; while (temp != 0) { temp = temp / 5; count += temp; } return count; }
在阶乘中,出现凡是出现5的都会与偶数化简的2相乘变成10,从而尾部就多了一个0。
例如25的阶乘,
1-4都没有5,这里0个,
5可以和2变成10,所以这里1个,
6-9,不可以化出5,
10=2*5,这里1个5,所以1个,
15=3*5,1个,
20=4*5,一个,
25=5*5,注意了,这里就有2个了,两个5可以和偶数组合成两个10相乘
所以25 = 2+1+1+1+1 = 6个0
可以化简成 25/5 + 25/5/5 = 6。
公式为:n/5 + n/5/5 + ... + 0