从零开始的Python ACM Ch.7:整数练习
黑洞数
- 黑洞数又称陷阱数,是类具有奇特转换特性的整数。任何一个数字不全相同整数,经有限“重排求差”操作,总会得某一个或一些数,这些数即为黑洞数。“重排求差”操作即把组成该数的数字重排后得到的最大数减去重排后得到的最小数。或者是冰雹原理中的“1”黑洞数
- 求出三位数以内的黑洞数
1 | blackhole_num = set({}) |
输出
1 | {495} |
我还去百度了一下,三位数的黑洞数确实只有495……
勾股数
- 这个应该不用多介绍了吧……输出100以内的所有勾股数
1 | # 暴力做法 |
输出(共52个)
1 | [(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (9, 40, 41), (10, 24, 26), (11, 60, 61), (12, 16, 20), (12, 35, 37), (13, 84, 85), (14, 48, 50), (15, 20, 25), (15, 36, 39), (16, 30, 34), (16, 63, 65), (18, 24, 30), (18, 80, 82), (20, 21, 29), (20, 48, 52), (21, 28, 35), (21, 72, 75), (24, 32, 40), (24, 45, 51), (24, 70, 74), (25, 60, 65), (27, 36, 45), (28, 45, 53), (28, 96, 100), (30, 40, 50), (30, 72, 78), (32, 60, 68), (33, 44, 55), (33, 56, 65), (35, 84, 91), (36, 48, 60), (36, 77, 85), (39, 52, 65), (39, 80, 89), (40, 42, 58), (40, 75, 85), (42, 56, 70), (45, 60, 75), (48, 55, 73), (48, 64, 80), (51, 68, 85), (54, 72, 90), (57, 76, 95), (60, 63, 87), (60, 80, 100), (65, 72, 97)] |
不重复的三位数
- 用1、2、3、4能组成多少个互不相同且无重复数字的三位数?都是多少?
- (其实Python里面的
itertools
里面有个方法叫做permutation()
1 | from itertools import permutations |
输出
1 | [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432] |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment