首页 > 代码库 > Python狂练-1

Python狂练-1

有四个数字: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) ):#异或
        print a+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