1.5 计算阶乘末尾0的个数

本文介绍了一种高效计算任意正整数阶乘末尾零的数量的算法。通过分析末尾零的产生原理,即由因数2和5相乘形成10导致,由于2的倍数通常多于5的倍数,只需统计5及其幂次作为因子出现的次数。给出了两种实现方法:一种是直观的逐个检查每个5的倍数;另一种更高效的算法则采用单层循环计算总数量。
1.5 Write an algorithm which computes the number of trailing zeros in n factorial.
EXAMPLE
input: 11

output: 2 (11! = 39916800)


末尾的0,来源肯定是某个数*10,而10=2*5。因为2总是比5个数多,所以只需要统计出因数5的个数即可。

比如1-11,包含5的有5和10,所以末尾有2个0。

25中包含2个5!

由此可以写出算法

<pre name="code" class="python">def main():
	n=input()
	ans=0
	for i in range(5,int(n)+1,5):
		j=i
		while (j>=5 and j%5==0):  //<span style="font-family: Arial, Helvetica, sans-serif;">j%5==0 don't forget </span>
			j=j/5
			ans+=1
	print(ans)

if __name__=='__main__':
	main()


答案的算法更好一点,只有一层循环

Note that while 5 contributes to one multiple of 10, 25 contributes two (because 25 = 5*5).

int NumOfTrailingZeros(int num) {
int count = 0;
if (num < 0) {
    printf(“Factorial is not defined for negative numbers\n”);
    return 0;
    }
for (int i = 5; num / i > 0; i *= 5) {
    count += num / i;
    }
return count;
}


检查以下代码问题:<template> <div class="article-detail-page"> <nav class="nav"><span @click="$router.back()" class="back"><</span> 面经详情</nav> <header class="header"> <!-- articles.stem 渲染不出来 --> <h1>1{{ articles.stem }}</h1> <p>{{ articles.createdAt }} | 315 浏览量 | 44 点赞数</p> <p> <img src="http://teachoss.itheima.net/heimaQuestionMiniapp/%E5%AE%98%E6%96%B9%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F%402x.png" alt="" /> <span>青春少年</span> </p> </header> <main class="body"> 虽然百度这几年发展势头落后于AT, 甚至快被京东赶上了,毕竟瘦死的骆驼比马大, 面试还是相当有难度和水准的, 一面..... </main> </div> </template> <script> // 请求地址: https://mock.boxuegu.com/mock/3083/articles/:id // 请求方式: get // import axios from 'axios' export default { name: "ArticleDetailPage", data() { return { articles:[ { id:'41156', createdAt: '2022-01-20', creatorName:'xiao仙女', stem:'老生常谈H5秒开', content:'实现 H5 应用的“秒开”体验,并确保在发布新版本时用户能够及时加载到最新的内容。这涉及到前端性能优化、缓存策略以及版本控制等多个方面', creatorAvatar:'http://teachoss.itheima.net/heimaQuestionMiniapp/%E5%AE%98%E6%96%B9%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F%402x.png', likeCount:'24', views:'212', }, { id:'41157', createdAt: '2023-01-22', creatorName:'青春, 那么骚', stem:'百度前端面经', content:'虽然百度这几年发展势头落后于AT,甚至快被京东赶上了,毕竟瘦死的骆驼比马大,面试还是相当有难度和水准的。一面1.询问你的项目经验、学习经历、主修语言(照实答)2.解释ES6的暂时性死区( let 和 var 的区别)3.箭头函数、闭包、异步(老生常谈,参见上文)4.高阶函数(呃……我真不太清楚这是啥,听起来挺像闭包的)5.求N的阶乘末尾有多少个0,在线码代码或讲思路(求因数,统计2、510个数', creatorAvatar:'http://teachoss.itheima.net/heimaQuestionMiniapp/%E5%AE%98%E6%96%B9%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F%402x.png', likeCount:'44', views:'315', }, { id:'41158', createdAt: '2024-08-20', creatorName:'前端琳琅阁ing', stem:'cesium中geojson数据渲染', content:'使用Cesium.GeoJsonDataSource.load(file,option)加载geojson文件、或者是geojson的数据,遍历每个区域使用viewer.entities.add()用于添加中心点的图标。使用viewer.entities.remove(entity)可以移除实体。', creatorAvatar:'http://teachoss.itheima.net/heimaQuestionMiniapp/%E5%AE%98%E6%96%B9%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F%402x.png', likeCount:'66', views:'216', } ] } }, created(){ console.log(this.$route.params.id) this.articles = this.articles.filter(item => this.$route.params.id === item.id ) console.log(this.articles) } } </script> <style lang="less" scoped> .article-detail-page { .nav { height: 44px; border-bottom: 1px solid #e4e4e4; line-height: 44px; text-align: center; .back { font-size: 18px; color: #666; position: absolute; left: 10px; top: 0; transform: scale(1, 1.5); } } .header { padding: 0 15px; p { color: #999; font-size: 12px; display: flex; align-items: center; } img { width: 40px; height: 40px; border-radius: 50%; overflow: hidden; } } .body { padding: 0 15px; } } </style>
07-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值