您的位置:首页 > 其它

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

2018-03-31 19:53 176 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: