【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))]

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM