当前位置:网站首页>【每周一坑】计算100以内质数之和 +【解答】输出三角形
【每周一坑】计算100以内质数之和 +【解答】输出三角形
2022-07-06 12:24:00 【Crossin的编程教室】
零基础python入门教程:python666.cn
大家好,欢迎来到 Crossin的编程教室 !
今天的题目很短:
计算1到100以内所有质数的和
所谓质数,就是除了1和此数自身外,不被其他自然数整除的数。
判断一个数是不是质数,这个不算难
找出1~100的范围内,所有的质数,这个也很简单
把找出来的质数加一起,这就更没难度啦
把上面3步合在一起就OK啦!
不过如果你有兴趣的话,可以进一步考虑一下你所用方法的算法复杂度是多少,看看谁的方法更简单。
另外,对于这道题,我有一种有意思的解法,并不完全按照上述这个思路,下周来公布。(不知道有没有人答案会跟我想的一样)
详细解答和参考代码将在下期栏目中给出,也可以参考其他同学在留言中的代码。
期待各位同学提交解答,更期待你能完成整个系列。
简单代码可直接在留言中提交,较长代码推荐使用 paste.ubuntu.com 或
codeshare.io 等代码分享网站,只需将代码复制上去保存,即可获得一个分享地址,非常方便。
往期问题可点击文章开头的合集“每周一坑”进入查看。
【解答】输出三角形
本题的关键是计算好每一行的空格数与当前行数的对应关系。
输出多个空格,可以通过循环,也可以直接通过“字符串乘法”实现,即:
'原始字符串' * n
可以得到 n 个 '原始字符串' 组成的新字符串。
参考答案:(来自 @KE10)
def ntriangle(n):
print('*'*n)
n -= 1
while n > 1:
print('*'+' '*(n-2)+'*')
n -= 1
print('*')
ntriangle(5)
_往期文章推荐_
如需了解付费精品课程及教学答疑服务
请在Crossin的编程教室内回复: 666
边栏推荐
- Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
- 案例 ①|主机安全建设:3个层级,11大能力的最佳实践
- 句号压缩过滤器
- 微信小程序常用集合
- Initial experience of addresssanitizer Technology
- B-jiege's tree (pressed tree DP)
- Error analysis ~csdn rebound shell error
- How does kubernetes support stateful applications through statefulset? (07)
- mod_ WSGI + pymssql path SQL server seat
- Cesium 两点之间的直线距离
猜你喜欢
随机推荐
Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
Oceanbase Community Edition OBD mode deployment mode stand-alone installation
BUUCTF---Reverse---easyre
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
mod_ WSGI + pymssql path SQL server seat
Node. Js: express + MySQL realizes registration, login and identity authentication
Cesium Click to draw a circle (dynamically draw a circle)
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
PowerPivot - DAX (first time)
String长度限制?
golang的超时处理使用技巧
Catch ball game 1
Color is converted to tristimulus value (r/g/b) (dry stock)
Leetcode question 283 Move zero
PHP and excel phpexcel
【GET-4】
Appx代码签名指南
js获取浏览器系统语言
腾讯字节阿里小米京东大厂Offer拿到手软,老师讲的真棒