这道题想了半天,知道用for循环,没想用嵌套的方式。还是参考了别人的答案
#for循环嵌套
total = 0
for units in range(1,5): #个位
for tens in range(1,5): #十位
for hundreds in range(1,5): #百位
if units != tens and tens != hundreds and units != hundreds:
sum = hundreds * 100 + tens * 10 + units
if sum >= 100 and sum <1000:
total+= 1
print("这些数分别是: " ,sum)
print("总共有 %d 个数" % (total))
本文介绍了一个使用Python for循环嵌套生成三个位数各不相同且在1到5之间的三唯一数的算法。通过控制个位、十位和百位数字的范围,确保生成的数字在100至999之间,且每位数字都不相同。
4605

被折叠的 条评论
为什么被折叠?



