题目描述:
/**
输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2
输入描述:
输入为一行,n(1 ≤ n ≤ 1000)
输出描述:
输出一个整数,即题目所求
输入例子1:
10
输出例子1:
2
*/
思路如下:
算出n含有多少个2因子,n含有多少个5因子,取两者较小值即可
代码如下:
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int N, numOf2=0, numOf5=0;
scanf("%d", &N);
for(int i=1; i<=N; i++){
int temp;
temp=i;
while(temp && temp%2==0){
temp/=2;
numOf2++;
}
temp=i;
while(temp && temp%5==0){
temp/=5;
numOf5++;
}
}
printf("%d", min(numOf2, numOf5));
return 0;
}
网友评论