1,2,3,4,5这五个数字可以组成多少个不重复的三位数,请用一个列表把所有可能列举出来。python代码输出
时间: 2024-05-10 07:19:21 浏览: 203
可以使用嵌套循环来解决这个问题。外层循环枚举百位数字,内层循环枚举十位数字和个位数字,并且需要保证三个数字不重复。
以下是Python代码:
```python
numbers = [1, 2, 3, 4, 5]
result = []
for i in numbers:
for j in numbers:
if i != j:
for k in numbers:
if i != k and j != k:
result.append(i * 100 + j * 10 + k)
print(result)
```
输出结果为:
```
[123, 124, 125, 132, 134, 135, 142, 143, 145, 152, 153, 154, 213, 214, 215, 231, 234, 235, 241, 243, 245, 251, 253, 254, 312, 314, 315, 321, 324, 325, 341, 342, 345, 351, 352, 354, 412, 413, 415, 421, 423, 425, 431, 432, 435, 451, 452, 453, 512, 513, 514, 521, 523, 524, 531, 532, 534, 541, 542, 543]
```
相关问题
python 1,2,3,4,5这五个数字可以组成多少个不重复的三位数,请用一个列表把所有可能枚举出来
可以使用嵌套循环来实现。外层循环遍历百位数字,中间循环遍历十位数字,内层循环遍历个位数字。然后将三个数字组合成一个三位数,加入到列表中。代码如下:
```python
numbers = [1, 2, 3, 4, 5]
result = []
for i in numbers:
for j in numbers:
for k in numbers:
if i != j and j != k and i != k:
num = i * 100 + j * 10 + k
result.append(num)
print(result)
```
输出结果:
```
[123, 124, 125, 132, 134, 135, 142, 143, 145, 152, 153, 154, 213, 214, 215, 231, 234, 235, 241, 243, 245, 251, 253, 254, 312, 314, 315, 321, 324, 325, 341, 342, 345, 351, 352, 354, 412, 413, 415, 421, 423, 425, 431, 432, 435, 451, 452, 453, 512, 513, 514, 521, 523, 524, 531, 532, 534, 541, 542, 543]
```
1,2,3,4,5这五个数字可以组成多少个不重复的三位数,请用一个列表把所有可能枚举出来,用python输出
可以使用嵌套的三重循环来实现:
```python
numbers = [1, 2, 3, 4, 5]
result = []
for i in numbers:
for j in numbers:
for k in numbers:
if i != j and i != k and j != k:
result.append(i*100+j*10+k)
print(result)
```
输出结果为:
```
[123, 124, 125, 132, 134, 135, 142, 143, 145, 152, 153, 154, 213, 214, 215, 231, 234, 235, 241, 243, 245, 251, 253, 254, 312, 314, 315, 321, 324, 325, 341, 342, 345, 351, 352, 354, 412, 413, 415, 421, 423, 425, 431, 432, 435, 451, 452, 453, 512, 513, 514, 521, 523, 524, 531, 532, 534, 541, 542, 543]
```
阅读全文