当前位置:网站首页>【每周一坑】计算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

边栏推荐
- Period compression filter
- 深度学习分类网络 -- ZFNet
- Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model
- Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
- Notes on beagleboneblack
- PHP and excel phpexcel
- Groovy基础语法整理
- Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
- AsyncHandler
- Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
猜你喜欢

An East SMS login resurrection installation and deployment tutorial

B-jiege's tree (pressed tree DP)

Case ① | host security construction: best practice of 3 levels and 11 capabilities

Tencent T3 teaches you hand in hand. It's really delicious

HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
Tencent architects first, 2022 Android interview written examination summary

语音识别(ASR)论文优选:全球最大的中英混合开源数据TALCS: An Open-Source Mandarin-English Code-Switching Corpus and a Speech

Web security - payload

Standardized QCI characteristics

BeagleBoneBlack 上手记
随机推荐
Cesium Click to draw a circle (dynamically draw a circle)
Event center parameter transfer, peer component value transfer method, brother component value transfer
报错分析~csdn反弹shell报错
Web开发小妙招:巧用ThreadLocal规避层层传值
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
PHP and excel phpexcel
POJ 3207 Ikki' s Story IV – Panda' s Trick (2-SAT)
Notes on beagleboneblack
JVM_ Common [interview questions]
Cesium 点击绘制圆形(动态绘制圆形)
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
BeagleBoneBlack 上手记
【每周一坑】输出三角形
Logstash expressway entrance
Unity writes a timer tool to start timing from the whole point. The format is: 00:00:00
Catch ball game 1
AsyncHandler
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
Standardized QCI characteristics