码迷,mamicode.com
首页 > 编程语言 > 详细

Python狂练-1

时间:2017-07-01 10:00:13      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:range   复数   code   重复数   usr   blog   div   多少   异或   

有四个数字: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

标签:range   复数   code   重复数   usr   blog   div   多少   异或   

原文地址:http://www.cnblogs.com/R4mble/p/7101206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!