今天起,每天一到算法,用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代码解决了一个组合数学问题:使用数字1、2、3、4可以组成多少个互不相同且无重复数字的三位数,并列举了所有可能的组合。

被折叠的 条评论
为什么被折叠?



