zoukankan      html  css  js  c++  java
  • 【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))]

     
  • 相关阅读:
    mall
    将UNICODE编码转换为中文
    460. LFU Cache
    957. Prison Cells After N Days
    455. Assign Cookies
    453. Minimum Moves to Equal Array Elements
    434. Number of Segments in a String
    1203. Sort Items by Groups Respecting Dependencies
    641. Design Circular Deque
    441. Arranging Coins
  • 原文地址:https://www.cnblogs.com/peiqianggao/p/5153885.html
Copyright © 2011-2022 走看看