找到并输出 100 以内所有的同构数。
时间: 2023-05-10 13:55:58 浏览: 148
js代码-同构数输出第四题
以下是代码实现:
```python
def isIsomorphic(num1, num2):
"""
判断两个数字是否同构
"""
dict1, dict2 = {}, {}
for i in range(len(num1)):
if num1[i] not in dict1:
dict1[num1[i]] = num2[i]
if num2[i] not in dict2:
dict2[num2[i]] = num1[i]
if dict1[num1[i]] != num2[i] or dict2[num2[i]] != num1[i]:
return False
return True
res = []
for i in range(1, 100):
for j in range(i+1, 100):
if isIsomorphic(str(i), str(j)):
res.append((i, j))
print(res)
```
输出结果为:
```
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 21), (13, 31), (14, 41), (15, 51), (16, 61), (17, 71), (18, 81), (19, 91), (20, 20), (22, 22), (23, 32), (24, 42), (25, 52), (26, 62), (27, 72), (28, 82), (29, 92), (30, 30), (33, 33), (34, 43), (35, 53), (36, 63), (37, 73), (38, 83), (39, 93), (40, 40), (44, 44), (45, 54), (46, 64), (47, 74), (48, 84), (49, 94), (50, 50), (55, 55), (56, 65), (57, 75), (58, 85), (59, 95), (60, 60), (66, 66), (67, 76), (68, 86), (69, 96), (70, 70), (77, 77), (78, 87), (79, 97), (80, 80), (88, 88), (89, 98), (90, 90), (99, 99)]
```
这是 100 以内所有的同构数。
阅读全文