有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

今天起,每天一到算法,用python编码!!!

2017-12-22 19:04 

# -*- coding: UTF-8 -*-

"""
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
"""


def PermutationAndCombination():
    a = [1, 2, 3, 4]
    result = ''
    count = 0
    for i in a:
        for j in a:
            if i == j:
                continue
            else:
                result = str(i) + '' + str(j)
                for k in a:
                    if k != i and k != j:
                        result_temp = result
                        result_temp += str(k)
                        print(result_temp)
                        count += 1
    return count


if __name__ == '__main__':
    print(PermutationAndCombination())


输出结果为:

123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432
24

解法2:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            if( i != k ) and (i != j) and (j != k):
                print i,j,k



你可能感兴趣的:(python算法题)