N!末尾有多少个 0呢?
N!=1×2×⋯×N。
代码框中的代码是一种实现,请分析并填写缺失的代码。
注意:求尾数等于多少0 ?可以看为该数能够被五整除后的数为多少
如100 ;
1,100 / 5 = 20 -----> ans = 20;
2,20 / 5 = 4 ------> ans = 20 + 4;
3, 4 并不能整除5 so.......ans = 24;
递归的方法:
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
	int n;
	scanf("%d" , &n);
	int ans = 0;
	while(n)
	{
		if(n % 5 == 0)
		{
			ans += n / 5;
			n = n / 5;
		}
		else
		{
			break;
		}
	}
	printf("%d" , ans);
	return 0;
} 答案:n = n / 5;
#include <iostream>
using namespace std;
int main() {
    int n, ans = 0;
    cin >> n;
    while (n) {
        ans += n = n/5;
    }
    cout << ans << endl;
    return 0;
} 
京公网安备 11010502036488号