当前位置:网站首页>【每周一坑】计算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
- 【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- Digital triangle model acwing 1018 Minimum toll
- Tencent cloud database public cloud market ranks top 2!
- 精彩编码 【进制转换】
- Problems encountered in using RT thread component fish
- POJ 3207 Ikki' s Story IV – Panda' s Trick (2-SAT)
- 腾讯字节阿里小米京东大厂Offer拿到手软,老师讲的真棒
- 22-07-05 upload of qiniu cloud storage pictures and user avatars
猜你喜欢
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
持续测试(CT)实战经验分享
Notes on beagleboneblack
小孩子学什么编程?
[network planning] Chapter 3 data link layer (3) channel division medium access control
PowerPivot - DAX (first time)
22-07-05 upload of qiniu cloud storage pictures and user avatars
Discussion on beegfs high availability mode
Standardized QCI characteristics
腾讯T4架构师,android面试基础
随机推荐
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
5. Wireless in vivo nano network: top ten "feasible?" problem
Catch ball game 1
RT thread I2C tutorial
Linear distance between two points of cesium
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
Standardized QCI characteristics
Tencent architects first, 2022 Android interview written examination summary
02 基础入门-数据包拓展
【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
recyclerview gridlayout 平分中间空白区域
Wechat applet common collection
5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
golang的超时处理使用技巧
【每周一坑】输出三角形
Color is converted to tristimulus value (r/g/b) (dry stock)
句号压缩过滤器
JS implementation force deduction 71 question simplified path
An East SMS login resurrection installation and deployment tutorial