1. 程式人生 > >Python_每日習題——0001

Python_每日習題——0001

ren numbers total emp out nbsp ica top RoCE

# 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