当前位置:网站首页>有序排列
有序排列
2022-06-26 07:38:00 【算法与编程之美】
引言
在学习数学的过程中,我们有时会遇到排列组合的问题。题目中给出几个元素,求出这几个元素一共可以有多少种不同的组合。而现在我们运用python中的for循环可以快速得出结果,并且一一输出这些组合。
问题
有1,2,3,4四个数字求四个数字能生成多少个互不相同且无重复数字的三位数(不能含有122,133类似),同时输出这些三位数字。
方法
首先将这几个数字作为一个字符串,再用for循环遍历这个字符串,要求三位数字就嵌套三层for循环(注意缩进),再使用if语句判断数字是否重复。最后设置一个变量作为计数器,统计一共有多少种组合。
实验结果与讨论
通过实验、实践等证明提出的方法是有效的,是能够解决开头提出的问题。
代码清单 1
| m='1234' n=0 for i in m: for j in m: for q in m: if (i!=j) and (j!=q) and (q!=i): n += 1 print(i,j,q) print('有',n,'个') |
结语
针对本次的有序排列问题,提出for循环嵌套的方法,通过实验,证明该方法是有效的。由于嵌套了三层循环,运行时的速度会有所下降。而且一旦所求组合的元素过多,需要嵌套更多的循环。未来可以探寻其他的方法来解决此类排列问题。
边栏推荐
- Sanic based services use celery to complete dynamic modification timing tasks
- MySQL storage and custom functions
- The first multi platform webcast of 2021ccf award ceremony pays tribute to the winners! CCF outstanding engineer
- 安装homebrew报错汇总
- 数据中心灾难恢复的重要参考指标:RTO和RPO
- Tetra - (4-pyridyl) porphyrin tpyp and metal complexes zntpyp/fetpyp/mntpyp/cutpyp/nitpyp/cotpyp/ptpyp/pdtpyp/cdtpyp (supplied by Qiyue porphyrin)
- 3D porphyrin MOF (mof-p5) / 3D porphyrin MOF (mof-p4) / 2D cobalt porphyrin MOF (ppf-1-co) / 2D porphyrin COF (POR COF) / supplied by Qiyue
- [UVM foundation] UVM_ Driver member variable req definition
- 信息学奥赛一本通 1355:字符串匹配问题(strs)
- Kalman filter_ Recursive Processing
猜你喜欢

In interface testing, several methods to verify the success of deleting interfaces

This paper analyzes the use method and implementation principle of eventbus event bus

Apache InLong毕业成为顶级项目,具备百万亿级数据流处理能力!

Iron / zinc / copper / platinum metal complexes such as 5,10,15,20-tetra (4-hydroxyphenyl) porphyrin (THPP) / (thppfe) / (thppzn) / (thppcu) / (thpppt) - Qiyue R & D
![[UVM basics] TLM common data receiving and sending and data receiving examples](/img/4f/6c6e8b26124ba042f949291b944c3d.jpg)
[UVM basics] TLM common data receiving and sending and data receiving examples
![Jemter stress test - Basic request - [teaching]](/img/f4/36dbd80e89d96e1121a6e2b92d1d07.png)
Jemter stress test - Basic request - [teaching]
![[North Asia data recovery] a server data recovery method in which the partitions in the RAID5 array are formatted due to the misoperation of the NTFS file system](/img/4d/01310b489ca6a599a125e849ae4856.jpg)
[North Asia data recovery] a server data recovery method in which the partitions in the RAID5 array are formatted due to the misoperation of the NTFS file system

Okhttp3 source code explanation (IV) cache strategy, disadvantages of Android mixed development

My colleague asked a question I never thought about. Why did kubernetes' superfluous' launch the static pod concept?

职场“大冤种”,不仅身累,心也被掏空……
随机推荐
一文搞懂Glide,不懂来打我
Jemter 壓力測試 -基礎請求-【教學篇】
Yyds dry inventory kubernetes easy service discovery and load balancing (11)
十大证券公司哪个佣金手续费最低,最安全可靠?
5,10,15,20-tetra (4-methoxycarbonylphenyl) porphyrin tcmpp purple crystal; Meso-5,10,15,20-tetra (4-methoxyphenyl) porphyrin tmopp|zn[t (4-mop) p] and co[t (4-mop) p] complexes
Excel中Unicode如何转换为汉字
MXNet对NIN网络中的网络的实现
OSPF design principles, commands take H3C as an example
Young man, do you know the original appearance of kotlin association process?
es 中 mapping 简介
Class class of box selection four to and polygon box selection based on leaflet encapsulation
php array_ Merge details
Scratch program learning
MySQL'replace into'has a self incrementing ID of the pit. There is a problem with the backup opportunity
Error: the specified LINQ expression contains a reference to a query associated with a different context
Teach you how to use the harmonyos local simulator
Jemter stress test - Basic request - [teaching]
Redis(4)----浅谈整数集合
Multisensor fusion sensing
Jemter stress test - basic requirements - [teaching]