求n!的末尾0的个数。

/*求阶乘的末尾的0的个数。
5!的末尾1个0
10!的末尾2个0
15!的末尾3个0
20!的末尾4个0
而25!的末尾6个0
    .
    .
    .
    .
知求n!的末尾的0的个数应为
n/5+n/(5^2)+n/(n^3)+......
直到n/(5^i)=0为止。(i=1,2,3,....)。
如求523!的末尾0的个数
523/5=104
523/25=20
523/125=4
523/625=0
故523!的末尾有104+20+4=128个0。
具体代码如下(仅供参考)
*/
#include<iostream>
using namespace std;
int main()
{
    int n,s=0,t=5;       //
    cin>>n;
    cout<<endl;
    while(t<=n)
    {
      s+=n/t;
      t=t*5;          //5的i次方(i=1,2,3.......)。    
    }
    cout<<s<<endl;
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qq_42387291/article/details/82721785
今日推荐