Python basic algorithm collection (3) find a unique combination of numbers and count how many such numbers there are

In the previous article, we used a recursive method to write the Fibonacci sequence. In this issue, we write a combination of numbers that are not repeated:

There are four numbers like this: 1, 2, 3, and 4. It is required to form a three-digit number that is not repeated, and how many such numbers are counted at the end?

Obviously, use exhaustive laws and examples to list all the possibilities, and then compare the numbers cited bit by bit. The procedure is as follows:

#有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
sum=0
for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            if i!=j and i!=k and j!=k:
                sum+=1
                print('{0}{1}{2}'.format(i,j,k))
print('可得不重复得数字有{}'.format(sum))

The program execution effect is as follows:

Exhaustive list of three digits without repetition
Exhaustive method to find non-repeated three-digit numbers

 

Second half:

Ask the company to issue bonuses

Guess you like

Origin blog.csdn.net/weixin_43115314/article/details/113931503