【python】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
# encoding:utf-8 # p001_1234threeNums.py def threeNums(): '''题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?''' print None count = 0 nums = [] for index1 in xrange(1,5): for index2 in xrange(1,5): for index3 in xrange(1,5): if index1 != index2 and index1 != index3 and index2 != index3: num = 100 * index1 + 10 * index2 + index3 if num not in nums: nums.append(num) count += 1 print count print nums # threeNums() # 在四个数中任意剔除一个,剩下三个的所有组合 --- 没完成,待完善 def threeNums_method1(): '''take out a digit from the four digits''' L = [i for i in xrange(1,5)] print L cnt = 0 for index in xrange(4): L1 = L[:] del L1[index] for index1 in xrange(3): print '%d%d%d'%(L1[index1%3],L1[(index1+1)%3],L1[(index1+2)%3]) cnt += 1 print 'count : %d'%cnt threeNums_method1()
################################################################
# 最简单方法
print [(x, y, z) for x in xrange(1,5) for y in xrange(1,5) for z in xrange(1,5) if ((x != y) and (y != z) and (x != z))]