//给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数。
1 #include <iostream> 2 using namespace std; 3 4 int GetN_1(int n) 5 { 6 if (n < 5) 7 { 8 return 0; 9 } 10 else 11 { 12 return (n / 5 + GetN_1(n / 5));//利用递归找一个阶乘中末尾零的个数 13 } 14 } 15 int GetN_2(int n)//采用循环的方式找一个阶乘中末尾零的个数 16 { 17 int m, count=0; 18 for(int i=1;i<=n;i++) 19 { 20 m=i; 21 while(m%5==0) 22 { 23 count++; 24 m/=5; 25 } 26 27 } 28 return count; 29 } 30 int main() 31 { 32 cout << GetN_1(1000) << endl; 33 cout << GetN_2(1000) << endl; 34 35 return 0; 36 }
原文:https://www.cnblogs.com/delongzhang/p/11027442.html