当前位置:网站首页>有序排列
有序排列
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循环嵌套的方法,通过实验,证明该方法是有效的。由于嵌套了三层循环,运行时的速度会有所下降。而且一旦所求组合的元素过多,需要嵌套更多的循环。未来可以探寻其他的方法来解决此类排列问题。
边栏推荐
- $a && $b = $c what???
- Solution to the permission problem when NPM install -g serve reports an error
- 一文分析EventBus-事件总线的使用方法和实现原理
- Jemter 压力测试 -可视化工具支持-【安装篇】
- Tetradecanoxy tetraphenylporphyrin methacrylate mm-tpp-14c; Cetanoxy tetraphenyl porphyrin methacrylate mm-tpp-16c; Purple solid; Qiyue supply
- [UVM basics] TLM common data receiving and sending and data receiving examples
- A bold sounding and awesome operation - remake a Netflix
- Shengshi Haotong enterprise wechat sector creates a digital ecological community
- 记一次开发 pgadmin 时执行 Building the Web Assets 遇到的依赖安装问题
- Apache InLong毕业成为顶级项目,具备百万亿级数据流处理能力!
猜你喜欢
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
Tetra - (4-pyridyl) porphyrin tpyp and metal complexes zntpyp/fetpyp/mntpyp/cutpyp/nitpyp/cotpyp/ptpyp/pdtpyp/cdtpyp (supplied by Qiyue porphyrin)
[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
Redis series - five common data types day1-3
Liujinhai, chief architect of zhongang Mining: according to the analysis of fluorite supply and demand, it is estimated that the fluorine coating market has great potential
Dark red crystal meso-5,10,15,20-tetra (p-aminophenyl) cobalt porphyrin (co TAPP); Meso-5,10,15,20-tetra (p-aminophenyl) cobalt porphyrin no complex (TAPP co no) supplied by Qiyue
多传感器融合感知
If you don't understand, please hit me
Webrtc has become the official standard of W3C and IETF, and mobile terminal development
Jemter 压力测试 -可视化工具-【使用篇】
随机推荐
数据中心灾难恢复的重要参考指标:RTO和RPO
Shengshi Haotong enterprise wechat sector creates a digital ecological community
OSPF design principles, commands take H3C as an example
Liquid crystal texture diagram of purple solid mm-tpp-10c methacrylic acid decanoxy tetraphenyl porphyrin and mm-tpp-12c methacrylic acid dodecanoxy tetraphenyl porphyrin - Qi Yue display
Go language custom DNS resolver practice yyds dry inventory
Summary of domestic database examination data (continuously updated)
QTreeWidget And QTableWidget
Jemter stress test - visualization tool - [usage]
Sanic based services use celery to complete dynamic modification timing tasks
Google Earth Engine(GEE) 02-基本了解和学习资源
Solution to the problem of multi application routing using thinkphp6.0
INSERT IGNORE 与INSERT INTO的区别
两水先木示身为Unity3D职场人的个人觉悟
How to quickly merge multiple PDF files?
一文搞懂Glide,不懂来打我
Apache inlong graduated as a top-level project with a million billion level data stream processing capability!
解决 psycopg2.NotSupportedError: PQconninfo not available in libpq < 9.3
ES cluster_ block_ exception read_ only_ allow_ Delete question
Crosslinked porphyrin based polyimide ppbpi-2, ppbpi-1-cr and ppbpi-2-cr; Porous porphyrin based hyperbranched polyimide (ppbpi-1, ppbpi-2) supplied by Qiyue
Redis series - five common data types day1-3