Python狂練-1
阿新 • • 發佈:2017-07-01
for amp color div and print join odi ^c
有四個數字:1、2、3、4,能組成多少個互不相同且無重復數字的三位數?各是多少?
把數字變成字符再把字符拼起來:
#!/usr/bin/env python # -*- coding:utf-8 -*- x = 0 for i in range(1,5): for j in range(1,5): for k in range(1,5): if( i!= j)and (i != k) and (j != k): num = ‘‘.join(map(str,[i,j,k])) x += 1 print‘第‘+str(x)+‘個數是:‘+num print ‘共有‘+str(x)+‘個數滿足條件。‘
神奇的位運算:
#!/usr/bin/env python # -*- coding:utf-8 -*- # coding:utf-8 #從 00 01 10 到 11 10 01 for num in range(6,58): a = num >> 4 & 3 b = num >> 2 & 3 c = num & 3 if( (a^b) and (b^c) and (c^a) ):#異或 printa+1,b+1,c+1
num = 6 000110
000000 & 000011 = 000000 0
000001 & 000011 = 000001 1
000110 & 000011 = 000010 2
000001:1 000011:3 000010:2
True
1,2,3
num = 7 000111
000000 & 000011 = 000000 0
000001 & 000011 = 000001 1
000111 & 000011 = 000011 3
000001:1 000010:2 000011:3
True
1,2,4
Python狂練-1