怎么用用Python1-9九个数字,能组成互不相同且无重复数字的两位数,打印输出这些两位数?
时间: 2024-05-01 20:17:24 浏览: 218
可以使用以下代码实现:
```
for i in range(1, 10):
for j in range(1, 10):
if i != j:
print(str(i) + str(j))
```
输出结果为:
```
12
13
14
15
16
17
18
19
21
23
24
25
26
27
28
29
31
32
34
35
36
37
38
39
41
42
43
45
46
47
48
49
51
52
53
54
56
57
58
59
61
62
63
64
65
67
68
69
71
72
73
74
75
76
78
79
81
82
83
84
85
86
87
89
91
92
93
94
95
96
97
98
```
相关问题
怎么用用Python1-9九个数字,能组成互不相同且无重复数字的两位数,以列表形式打印输出这些两位数?
可以使用嵌套循环,代码如下:
```python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
result = []
for i in range(len(nums)):
for j in range(i+1, len(nums)):
num = int(str(nums[i]) + str(nums[j]))
result.append(num)
print(result)
```
输出结果为:
```python
[12, 13, 14, 15, 16, 17, 18, 19, 23, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 56, 57, 58, 59, 67, 68, 69, 78, 79, 89]
```
怎么用用Python1-9九个数字,能组成互不相同且无个位十位不同的两位数,以列表形式打印输出这些两位数?
可以使用如下代码实现:
```python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
results = []
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
num = int(str(nums[i]) + str(nums[j]))
if num % 11 != 0:
results.append(num)
print(results)
```
输出结果为:
```
[12, 13, 14, 15, 16, 17, 18, 19, 23, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 56, 57, 58, 59, 67, 68, 69, 78, 79, 89]
```
阅读全文