马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目描述
写一个程序,求N的阶乘(N!=1*2*3...N),求末尾0的个数。例如: 12! = 1*2*3*4...11*12=479001600末尾有2个0。所以输出2
输入描述
一个整数 N。
输出描述
N!末尾 0 的个数
样例1
输入
12
输出
2
对于 20%的数据有 $0<N<=15$对于 100% 的数据有 $0<N<=10000$
分析:一个数末尾有几个0,取决于它的质因数乘积形式有几个5和几个2,这两个数量的最小值就是0的个数。
#include<bits/stdc++.h>
using namespace std;
int countfactor(int a, int b){
int cnt=0;
while(a%b==0){
a/=b;
cnt+=1;
}
return cnt;
}
int main(){
int n;
cin>>n;
int twos=0,fives=0;
for(int i=1;i<=n;++i){
int two=countfactor(i,2);
int five=countfactor(i,5);
twos+=two;
fives+=five;
}
cout<<min(twos, fives);
return 0;
}
|