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

边栏推荐
- Le lancement du jupyter ne répond pas après l'installation d'Anaconda
- Unity load AB package
- 小微企业难做账?智能代账小工具快用起来
- How does kubernetes support stateful applications through statefulset? (07)
- Tencent cloud database public cloud market ranks top 2!
- 夏志刚介绍
- 【GET-4】
- 语音识别(ASR)论文优选:全球最大的中英混合开源数据TALCS: An Open-Source Mandarin-English Code-Switching Corpus and a Speech
- SSH connection denied
- In line elements are transformed into block level elements, and display transformation and implicit transformation
猜你喜欢
![[Yann Lecun likes the red stone neural network made by minecraft]](/img/95/c3af40c7ecbd371dd674aea19b272a.png)
[Yann Lecun likes the red stone neural network made by minecraft]

为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?

5. Wireless in vivo nano network: top ten "feasible?" problem

某东短信登录复活 安装部署教程
![[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN](/img/b8/3d48e185bb6eafcdd49889f0a90657.png)
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN

Node. Js: express + MySQL realizes registration, login and identity authentication
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发

Continuous test (CT) practical experience sharing
![[cloud native and 5g] micro services support 5g core network](/img/c9/4ccacd1e70285c2ceb50c324e5018c.png)
[cloud native and 5g] micro services support 5g core network

Node.js: express + MySQL实现注册登录,身份认证
随机推荐
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
PowerPivot——DAX(初识)
String length limit?
Le lancement du jupyter ne répond pas après l'installation d'Anaconda
报错分析~csdn反弹shell报错
【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
Enumeration gets values based on parameters
Error analysis ~csdn rebound shell error
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
Tencent T2 Daniel explained in person and doubled his job hopping salary
Catch ball game 1
Synchronization of data create trigger synchronization table for each site
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
Tencent Android development interview, basic knowledge of Android Development
小微企业难做账?智能代账小工具快用起来
Learn to punch in Web
Poj1149 pigs [maximum flow]
Standardized QCI characteristics
PHP与EXCEL PHPExcel