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

边栏推荐
- B-杰哥的树(状压树形dp)
- Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
- 02 基础入门-数据包拓展
- 报错分析~csdn反弹shell报错
- How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
- Qinglong panel white screen one key repair
- golang的超时处理使用技巧
- Poj1149 pigs [maximum flow]
- RT-Thread 组件 FinSH 使用时遇到的问题
- 【GET-4】
猜你喜欢

Event center parameter transfer, peer component value transfer method, brother component value transfer

Standardized QCI characteristics

RT thread I2C tutorial

Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model

Digital triangle model acwing 1018 Minimum toll

Node. Js: express + MySQL realizes registration, login and identity authentication

Web security - payload

5. 無線體內納米網:十大“可行嗎?”問題

Continuous test (CT) practical experience sharing

Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
随机推荐
Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
recyclerview gridlayout 平分中间空白区域
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
POJ 3207 Ikki' s Story IV – Panda' s Trick (2-SAT)
PowerPivot——DAX(初识)
JVM_ Common [interview questions]
AsyncHandler
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
AsyncHandler
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
8086指令码汇总表(表格)
Wonderful coding [hexadecimal conversion]
Database specific interpretation of paradigm
Color is converted to tristimulus value (r/g/b) (dry stock)
01 基础入门-概念名词
Event center parameter transfer, peer component value transfer method, brother component value transfer
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
5. Wireless in vivo nano network: top ten "feasible?" problem
Initial experience of addresssanitizer Technology