当前位置:网站首页>实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
2022-07-05 08:16:00 【懒笑翻】
# 实例001:数字组合 # 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
环境:Pycharm2022 + Anaconda3
先用数学排列组合分析再用Python实现 并进行方法总结
目录
用数学排列组合分析
首先我想到的是排列数组合数,由于题目要求是无重复数字,因此为排列数。排列的定义:从n个不同元素中,任取m(m≤n,m与n均为自然数,下同)个不同的元素按照一定的顺序排成一列,叫做从n个不同元素中取出m个元素的一个排列;从n个不同元素中取出m(m≤n)个元素的所有排列的个数,叫做从n个不同元素中取出m个元素的排列数。
我们可以得出此题的排列数是 4! / (4-3)! = 4! = 24 (个)
Python实现
方法一:我们可以循环遍历3个数字,由于要求不重复数字,因此在输出时做出条件判断,只输出百位数十位数个位数都不相同的数。
arrange = 0 # 定义排列数
nums = range(1, 5)
# 方法一
for i in nums: # 百位数
for j in nums: # 十位数
for k in nums: # 个位数
if ((i != j) and (j != k) and (k != i)): # 只输出无重复数字
print(i, j, k)
arrange += 1
print(arrange)运行结果如下:
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 个
Process finished with exit code 0方法二:可以直接调用itertools的permutations()类,# itertools,是python的一个内置模块,功能强大,主要用于高效循环创建迭代器。注意一点,他返回的不是list,而是iterator
import itertools # itertools,是python的一个内置模块,功能强大,主要用于高效循环创建迭代器。注意一点,他返回的不是list,而是iterator
arrange = 0 # 定义排列数
nums = range(1, 5)
for i in itertools.permutations(nums, 3): # (迭代的元素,迭代序列长度)
print(i)
arrange += 1
print(arrange)运行结果如下:
(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方法二种用到的 itertools.permutations 类,该类内容如下:
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. """
passpermutations类创建一个迭代器,返回iterable中所有长度为r的项目序列,如果省略了r,那么序列的长度与iterable中的项目数量相同,即会返回一个全排列的结果。
边栏推荐
- Various types of questions judged by prime numbers within 100 (C language)
- Relationship between line voltage and phase voltage, line current and phase current
- Programming knowledge -- basis of C language
- Network communication model -- Network OSI tcp/ip layering
- DokuWiki deployment notes
- Briefly talk about the identification protocol of mobile port -bc1.2
- Bootloader implementation of PIC MCU
- Anonymous structure in C language
- STM32 single chip microcomputer - external interrupt
- Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
猜你喜欢

Keil use details -- magic wand
![C WinForm [display real-time time in the status bar] - practical exercise 1](/img/9f/d193cbb488542cc4c439efd79c4963.jpg)
C WinForm [display real-time time in the status bar] - practical exercise 1

Arduino uses nrf24l01+ communication

Development tools -- gcc compiler usage

STM32 single chip microcomputer -- volatile keyword
![Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine](/img/55/0f05291755dc1c3c03db8e991a1ba1.jpg)
Shape template matching based on Halcon learning [VII] reuse_ model. Hdev routine

Connection mode - bridge and net

Carrier period, electrical speed, carrier period variation

Consul installation

Management and use of DokuWiki (supplementary)
随机推荐
leetcode - 445. 两数相加 II
Factors affecting the quality of slip rings in production
Brief discussion on Buck buck circuit
Imx6ull bare metal development learning 2- use C language to light LED indicator
STM32 tutorial triple ADC interleaved sampling
MySQL之MHA高可用集群
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
C language enhancement -- pointer
Halcon's practice based on shape template matching [2]
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
Problem solving: interpreter error: no file or directory
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
STM32 --- configuration of external interrupt
Communication standard -- communication protocol
Void* C is a carrier for realizing polymorphism
General makefile (I) single C language compilation template
Connection mode - bridge and net
OC and OD gate circuit