当前位置:网站首页>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 .
边栏推荐
- STM32 single chip microcomputer -- volatile keyword
- C WinForm [help interface - send email] - practice five
- Drive LED -- GPIO control
- H264 (I) i/p/b frame gop/idr/ and other parameters
- Soem EtherCAT source code analysis II (list of known configuration information)
- Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
- FIO测试硬盘性能参数和实例详细总结(附源码)
- 实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
- Brief discussion on Buck buck circuit
- Talk about the function of magnetic beads in circuits
猜你喜欢
Various types of questions judged by prime numbers within 100 (C language)
QEMU STM32 vscode debugging environment configuration
STM32 --- serial port communication
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
MHA High available Cluster for MySQL
Stm32--- systick timer
实例002:“个税计算” 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.
Compilation warning solution sorting in Quartus II
Talk about the circuit use of TVs tube
随机推荐
Slist of linked list
实例006:斐波那契数列
DokuWiki deployment notes
Buildroot system for making raspberry pie cm3
Hardware 3 -- function of voltage follower
Hardware 1 -- relationship between gain and magnification
The firmware of the connected j-link does not support the following memory access
Relationship between line voltage and phase voltage, line current and phase current
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
What are the test items of power battery ul2580
DCDC circuit - function of bootstrap capacitor
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
Adaptive filter
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Solutions to compilation warnings in Quartus II
Hardware and software solution of FPGA key chattering elimination
[trio basic from introduction to mastery tutorial XIV] trio realizes unit axis multi-color code capture
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine