python练习题目(二)

题目一

ABCD乘9=DCBA,A=? B=? C=? D=?

程序分析

  • A和D肯定不为0,B和C取值范围为0-9
  • 以A开头的四位数乘9得到四位数,由于判断A肯定为1
  • A为1,那么乘以9得到的四位数,D肯定为9

    代码实现

    1
    2
    3
    4
    5
    6
    for A in range(1, 2):
    for B in range(0, 10):
    for C in range(0, 10):
    for D in range(9, 10):
    if (1000*A + 100*B + 10*C + D)*9 == (D*1000 + C*100 + B*10 + A):
    print("A={0},B={1},C={2},D={3}".format(A, B, C, D))

输出结果

python-cal-result

题目二

九宫格
A B C
D E F
G H I
A-I代表数字,取值范围为1-9,要求横、竖、对角各方向的3个数字不重复,且3个数字之和相等,均为15

程序分析

  • 数字取值范围为1-9
  • A取值范围1-9,那么B取值范围为1-9同时排除A
  • C取值范围1-9同时排除A和B,依次类推

    代码实现

    实现方式1

    使用copy()及remove()方法实现各数字判断
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    import time

    start = time.clock()
    number = [x for x in range(1, 10)]

    for A in number:
    a = number.copy()
    a.remove(A)
    for B in a:
    b = a.copy()
    b.remove(B)
    for C in b:
    c = b.copy()
    c.remove(C)
    for D in c:
    d = c.copy()
    d.remove(D)
    for E in d:
    e = d.copy()
    e.remove(E)
    for F in e:
    f = e.copy()
    f.remove(F)
    for G in f:
    g = f.copy()
    g.remove(G)
    for H in g:
    h = g.copy()
    h.remove(H)
    for I in h:
    if A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I == A+E+I == C+E+G == 15:
    print('''
    -------------
    | {0} | {1} | {2} |
    | {3} | {4} | {5} |
    | {6} | {7} | {8} |
    -------------'''.format(A, B, C, D, E, F, G, H, I))

    end = time.clock()
    print('Running time: %s Seconds'%(end-start))

实现方式2

使用列表生成式实现各数字判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import time

start = time.clock()
for A in [x for x in range(1, 10)]:
for B in [x for x in range(1, 10) if x != A]:
for C in [x for x in range(1, 10) if x != B and x != A]:
for D in [x for x in range(1, 10) if x != C and x != B and x != A]:
for E in [x for x in range(1, 10) if x != D and x != C and x != B and x != A]:
for F in [x for x in range(1, 10) if x != E and x != D and x != C and x != B and x != A]:
for G in [x for x in range(1, 10) if x != F and x != E and x != D and x != C and x != B and x != A]:
for H in [x for x in range(1, 10) if x != G and x != F and x != E and x != D and x != C and x != B and x != A]:
for I in [x for x in range(1, 10) if x != H and x != G and x != F and x != E and x != D and x != C and x != B and x != A]:
if A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I == A+E+I == C+E+G == 15:
print('''
-------------
| {0} | {1} | {2} |
| {3} | {4} | {5} |
| {6} | {7} | {8} |
-------------'''.format(A, B, C, D, E, F, G, H, I))
end = time.clock()
print('Running time: %s Seconds'%(end-start))

输出结果

实现方式1

python-jiucopy-result

实现方式2

python-jiulist-result

知识点

根据题目2输出结果中的运行时间可知,方式1运行0.3秒,方式2运行1.4秒,相差5倍。
根据代码逻辑,方式1在嵌套循环中直接使用remove()方式删除;方式2需要在嵌套循环中进行比较,这是导致两种算法时间差异的主要原因。
同理,题目1中通过对题目分析,确定数字A和D的取值,不需要从1-9进行循环,对程序运行时间肯定也有大幅的提升。
好代码好算法,值得关注。

Recommended Posts