loop #12
loop
#12
Replies: 5 comments
-
乘法表for i in range(1, 10):
for j in range(1, i + 1):
print(f"{j} * {i} = {j * i}", end="\t")
print() |
Beta Was this translation helpful? Give feedback.
0 replies
-
水仙花数# 示例:计算 100 到 999 范围内的水仙花数
start = 100
end = 999
for num in range(start, end + 1):
# 将数字转换为字符串,以便逐位处理
digits = list(map(int, str(num)))
# 获取数字的位数
num_digits = len(digits)
# 计算每位数字的 n 次方之和
sum_of_powers = sum([digit ** num_digits for digit in digits])
# 判断该和是否等于原数
if sum_of_powers == num:
print(num) |
Beta Was this translation helpful? Give feedback.
0 replies
-
猴子偷桃我们可以这样解决这个问题:假设第 # 假设第十天剩下 1 个桃子
days = 10
peaches = 1
# 递推计算前面的桃子数量
for day in range(days - 1, 0, -1):
peaches = 2 * (peaches + 1)
print(peaches) |
Beta Was this translation helpful? Give feedback.
0 replies
-
打印因数找到一个整数的因数的最简单方法就是从 1 开始,一个一个数试过去,如果这个数能够把给定的数整除,则它是给定的数的因数。程序如下: n = 1200
for i in range(1, n+1):
if n % i == 0:
print(i, end=", ")因数总是成对出现的,所以我们一可以成对查找它们: n = 1200
for i in range(1, int(n**0.5) + 1):
if n % i == 0:
print(i, n // i) |
Beta Was this translation helpful? Give feedback.
0 replies
-
质因数分解n = 1202
for i in range(2, n // 2 + 1):
while n % i == 0:
print(i, end=", ")
n = n // i |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
loop
条件语句在编程中用于根据一定的条件控制程序的执行流程,它会让程序根据不同的条件执行不同的代码块,是编程中实现决策逻辑的基本方式之一。
https://py.qizhen.xyz/loop
Beta Was this translation helpful? Give feedback.
All reactions