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

边栏推荐
- Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
- Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
- golang的超时处理使用技巧
- Continuous test (CT) practical experience sharing
- 01 基础入门-概念名词
- Boder radius has four values, and boder radius exceeds four values
- 为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
- AsyncHandler
- What happened to the kernel after malloc() was transferred? Attached malloc () and free () implementation source
- 【GET-4】
猜你喜欢

RT thread I2C tutorial

Number of schemes from the upper left corner to the lower right corner of the chessboard (2)

Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle

A5000 vgpu display mode switching

5. 无线体内纳米网:十大“可行吗?”问题

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

【每周一坑】输出三角形

【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】

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

【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
随机推荐
Guangzhou's first data security summit will open in Baiyun District
Digital triangle model acwing 1018 Minimum toll
Discussion on beegfs high availability mode
Linear distance between two points of cesium
永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
Standardized QCI characteristics
Notes on beagleboneblack
精彩编码 【进制转换】
【云原生与5G】微服务加持5G核心网
Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
Event center parameter transfer, peer component value transfer method, brother component value transfer
Unity writes a timer tool to start timing from the whole point. The format is: 00:00:00
【每周一坑】信息加密 +【解答】正整数分解质因数
Node. Js: express + MySQL realizes registration, login and identity authentication
String长度限制?
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
[network planning] Chapter 3 data link layer (3) channel division medium access control
02 基础入门-数据包拓展
5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
BUUCTF---Reverse---easyre