当前位置:网站首页>Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
2022-07-05 08:21:00 【Lazy smile】
# example 001: Number combination # There are four numbers :1、2、3、4, How many different and unrepeated three digit numbers can be formed ? How many are each ?
Environmental Science :Pycharm2022 + Anaconda3
First use mathematical permutation and combination analysis, and then Python Realization And summarize the methods
Catalog
Analyze with mathematical permutation and combination
Analyze with mathematical permutation and combination
The first thing I think of is arranging the array composite , Because the title requires no repeated numbers , Therefore, it is the number of permutations . Definition of permutation : from n Among the different elements , Take whatever you like m(m≤n,m And n All are natural numbers , The same below ) Different elements are arranged in a certain order , It's called from n Take out... Of the different elements m An arrangement of elements ; from n Take out... Of the different elements m(m≤n) The number of all permutations of elements , It's called from n Take out... Of the different elements m Number of permutations of elements .
We can conclude that the permutation of this question is 4! / (4-3)! = 4! = 24 ( individual )
Python Realization
Method 1 : We can loop through 3 A digital , Because it is required not to repeat numbers , Therefore, make conditional judgment when outputting , Only output hundreds of digits, tens of digits, and numbers with different single digits .
arrange = 0 # Define the number of permutations
nums = range(1, 5)
# Method 1
for i in nums: # Hundreds of digits
for j in nums: # Ten digits
for k in nums: # Single digit
if ((i != j) and (j != k) and (k != i)): # Only output numbers without repetition
print(i, j, k)
arrange += 1
print(arrange)
The operation results are as follows :
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
24 individual
Process finished with exit code 0
Method 2 : Can be called directly itertools Of permutations() class ,# itertools, yes python A built-in module for , Powerful , Mainly used for efficient loop creation iterators . Be careful. , He did not return list, It is iterator
import itertools # itertools, yes python A built-in module for , Powerful , Mainly used for efficient loop creation iterators . Be careful. , He did not return list, It is iterator
arrange = 0 # Define the number of permutations
nums = range(1, 5)
for i in itertools.permutations(nums, 3): # ( Iterating elements , Iteration sequence length )
print(i)
arrange += 1
print(arrange)
The operation results are as follows :
(1, 2, 3)
(1, 2, 4)
(1, 3, 2)
(1, 3, 4)
(1, 4, 2)
(1, 4, 3)
(2, 1, 3)
(2, 1, 4)
(2, 3, 1)
(2, 3, 4)
(2, 4, 1)
(2, 4, 3)
(3, 1, 2)
(3, 1, 4)
(3, 2, 1)
(3, 2, 4)
(3, 4, 1)
(3, 4, 2)
(4, 1, 2)
(4, 1, 3)
(4, 2, 1)
(4, 2, 3)
(4, 3, 1)
(4, 3, 2)
24
The second method is used itertools.permutations class , Such contents are as follows: :
class permutations(object):
"""
Return successive r-length permutations of elements in the iterable.
permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
"""
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __init__(self, range, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__
pass
def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __next__(self, *args, **kwargs): # real signature unknown
""" Implement next(self). """
pass
def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass
def __setstate__(self, *args, **kwargs): # real signature unknown
""" Set state information for unpickling. """
pass
def __sizeof__(self, *args, **kwargs): # real signature unknown
""" Returns size in memory, in bytes. """
pass
permutations Class to create an iterator , return iterable All lengths in are r Project sequence , If you omit r, Then the length of the sequence is the same as iterable The number of items in is the same , A full permutation result will be returned .
边栏推荐
- Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
- STM32 --- configuration of external interrupt
- go依赖注入--google开源库wire
- 实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- Wifi-802.11 negotiation rate table
- Explication de la procédure stockée pour SQL Server
- 实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
- Detailed explanation of SQL server stored procedures
- STM32 tutorial triple ADC interleaved sampling
猜你喜欢
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
Hardware 3 -- function of voltage follower
C WinForm [display real-time time in the status bar] - practical exercise 1
Management and use of DokuWiki (supplementary)
[paper reading] the latest transfer ability in deep learning: a survey in 2022
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
实例008:九九乘法表
Summary of SIM card circuit knowledge
Semiconductor devices (I) PN junction
Charge pump boost principle - this article will give you a simple understanding
随机推荐
Stm32--- systick timer
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
剑指 Offer 05. 替换空格
List of linked lists
Verilog -- state machine coding method
Basic information commands and functions of kernel development
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Summary of SIM card circuit knowledge
DCDC circuit - function of bootstrap capacitor
General makefile (I) single C language compilation template
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Slist of linked list
C language # and #
Talk about the function of magnetic beads in circuits
99 multiplication table (C language)
Tailq of linked list
Problem solving: interpreter error: no file or directory
MySQL MHA high availability cluster
Why is 1900 not a leap year
Void* C is a carrier for realizing polymorphism