poj1401--Factorial

来源:互联网 发布:python手册中文版 pdf 编辑:程序博客网 时间:2024/06/05 08:31
求的是N!中末尾0的个数,即包含因子10的个数。10=5*2,因子10的个数也就是因子2的个数与因子5的个数中较小的那个,N!=1*2*3...*N,其中2的个数一定不少于5的个数,所以要求的其实就是N!中5的个数。
#include <cstdio>#include <iostream>#include <cstdlib>using namespace std;int f(int n){      int s=0,i;      for(i=5;i<=n;i*=5)            s+=n/i;      return s;}int main(){      int t,n;      cin>>t;      while(t--)      {            cin>>n;            cout<<f(n)<<endl;      }      return 0;}

原创粉丝点击