题目:
给定一个整数 n,返回 n! 结果尾数中零的数量。
示例 1:
输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零。
示例 2:
输入: 5
输出: 1
解释: 5! = 120, 尾数中有 1 个零.
说明: 你算法的时间复杂度应为 O(log n) 。
/*
* 题目:阶乘后的0
* 算法:1、首先用递归算出n的阶乘
* 2、让n!对10取余,就能得到个位上的数字,然后判断
* 3、n!对10取模,舍弃个位的数
*/
int factorial(int n)
{
if (n == 1) return 1;
return n * (factorial(n - 1));
}
int count(int n)
{
int num = factorial(n);
printf("%d ", num);
int c = 0;
int t = 0;
while (num)
{
t = num % 10;
if (t == 0)
{
c++;
}
num /= 10;
}
return c;
}
int main()
{
int n;
printf("please input a number:");
scanf_s("%d", &n);
int c = count(n);
printf("%d\n", c);
return 0;
}