当前位置:网站首页>有序排列
有序排列
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循环嵌套的方法,通过实验,证明该方法是有效的。由于嵌套了三层循环,运行时的速度会有所下降。而且一旦所求组合的元素过多,需要嵌套更多的循环。未来可以探寻其他的方法来解决此类排列问题。
边栏推荐
- How MySQL implements the RC transaction isolation level
- 执行npm install -g serve时报错权限权限问题解决方案
- Xiaosha's counting (bit operation, Combinatorial Mathematics) - Niuke
- Getdrawingcache of view is empty. Solution: interview questions for Android audio and video development
- 手机开户哪个证券公司佣金最低?网上开户是否安全么?
- Take you three minutes to get started typescript
- GMP model
- Tetradecanoxy tetraphenylporphyrin methacrylate mm-tpp-14c; Cetanoxy tetraphenyl porphyrin methacrylate mm-tpp-16c; Purple solid; Qiyue supply
- Is it legal to open an account for compass stock trading software? Is it safe?
- js模块化
猜你喜欢

记一次开发 pgadmin 时执行 Building the Web Assets 遇到的依赖安装问题

Installation homebrew error summary

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

Junit

Young man, do you know the original appearance of kotlin association process?

MXNet对NIN网络中的网络的实现

Jemter 压力测试 -基础请求-【教学篇】
![[recommend an entity class conversion tool mapstruct, which is powerful and easy to use]](/img/7b/43becce42192fb5e0469465aa27a36.png)
[recommend an entity class conversion tool mapstruct, which is powerful and easy to use]

How to convert Unicode into Chinese characters in Excel

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
随机推荐
5,10,15,20-tetra (4-bromophenyl) porphyrin (h2tppbr4) /5.2.15,10,15,20-tetra [4-[(3-aminophenyl) ethynyl] phenyl] porphyrin (tapepp) Qiyue porphyrin reagent
Xiaosha's counting (bit operation, Combinatorial Mathematics) - Niuke
[UVM basics] understanding of sequence and sequencer
ES cluster_ block_ exception read_ only_ allow_ Delete question
[recommend 10 easy idea plug-ins with less tedious and repetitive code]
职场“大冤种”,不仅身累,心也被掏空……
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
MySQL storage and custom functions
GMP model
Redis(4)----浅谈整数集合
Request&Response
php array_ Merge details
Solution to the permission problem when NPM install -g serve reports an error
少年,你可知 Kotlin 协程最初的样子?
Okhttp3 source code explanation (IV) cache strategy, disadvantages of Android mixed development
The difference between insert ignore and insert into
1010. song backtracking with total duration divisible by 60
Jemter 压力测试 -可视化工具支持-【安装篇】
Yyds dry inventory kubernetes easy service discovery and load balancing (11)
C#/. Net phase VI 01C Foundation_ 02:vs2019 basic operations, excluding code files, smart tips, data types, differences between float and double, and differences between string and string