首页 > 编程语言 > 详细

Python_每日习题——0001

时间:2019-04-05 14:38:32      阅读:135      评论:0      收藏:0      [点我收藏+]
# Topic: There are four digits: 1, 2, 3 and 4.
# How many different three digits can be formed without repeating numbers? How much is each?


# Procedure analysis: traverse all possibilities and shave out duplicates.

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



# Simple Method: Use permutations in itertools

import itertools

sum2 = 0
a = [1,2,3,4]
for i in itertools.permutations(a,3):
    print(i)
    sum2 += 1
print(sum2)
#    permutations method emphasizes permutations

import itertools
n=int(raw_input())
a=[str(i) for i in range(n)]
s=""
s=s.join(a)
for i in itertools.permutations(s,n):
    print ‘‘.join(i)
#combinations method focuses on combination

import itertools
n=int(raw_input())
a=[str(i) for i in range(n)]
s=""
s=s.join(a)
for i in itertools.combinations(s,n):
    print ‘‘.join(i)

 

Python_每日习题——0001

原文:https://www.cnblogs.com/LXL616/p/10658608.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!