2018 蓝桥杯省赛 B 组模拟赛(五)末尾零的个数

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值