当前位置:网站首页>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 .
边栏推荐
- Vofa+ software usage record
- 实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
- Ble encryption details
- Detailed summary of FIO test hard disk performance parameters and examples (with source code)
- Stablq of linked list
- 关于线性稳压器的五个设计细节
- VESC Benjamin test motor parameters
- C WinForm [get file path -- traverse folder pictures] - practical exercise 6
- MySQL MHA high availability cluster
- Basic embedded concepts
猜你喜欢
Simple design description of MIC circuit of ECM mobile phone
Some thoughts on extracting perspectives from ealfa and Ebeta
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
H264 (I) i/p/b frame gop/idr/ and other parameters
DokuWiki deployment notes
NTC thermistor application - temperature measurement
Halcon's practice based on shape template matching [2]
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Why is 1900 not a leap year
【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
随机推荐
The firmware of the connected j-link does not support the following memory access
Bootloader implementation of PIC MCU
Briefly talk about the identification protocol of mobile port -bc1.2
Detailed explanation of SQL server stored procedures
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
实例007:copy 将一个列表的数据复制到另一个列表中。
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
STM32 --- serial port communication
WiFi wpa_ Detailed description of supplicant hostpad interface
STM32 --- GPIO configuration & GPIO related library functions
Working principle and type selection of common mode inductor
Solutions to compilation warnings in Quartus II
Explication de la procédure stockée pour SQL Server
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine
STM32 --- configuration of external interrupt
C WinForm [help interface - send email] - practice five
STM32 tutorial triple ADC interleaved sampling
Simple design description of MIC circuit of ECM mobile phone