python编程比赛--将1-8几个数字填写下边表格,要求每一列数字比右边大,每一行数字比下边大,有几种填法?
2021/10/31 11:10:48
本文主要是介绍python编程比赛--将1-8几个数字填写下边表格,要求每一列数字比右边大,每一行数字比下边大,有几种填法?,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python编程比赛–将1-8几个数字填写下边表格,要求每一列数字比右边大,每一行数字比下边大,有几种填法?
题目描述:
题目:将1-8几个数字填写下边表格,要求每一列数字比右边大,每一行数字比下边大,有几种填法?
代码
count=0 for a in range(1,9): for b in range(1, 9): for c in range(1, 9): for d in range(1, 9): for e in range(1, 9): for f in range(1, 9): for g in range(1, 9): for h in range(1, 9): if (a!=b and a!=c and a!=d and a!=e and a!=f and a!=g and a!=h and b!=c and b!=d and b!=e and b!=f and b!=g and b!=h and c != d and c != e and c != f and c != g and c != h and d != e and d != f and d != g and d != h and e != f and e != g and e != h and f != g and f != h and g!=f ): if ((a>b) and (b>c) and (c>d)and(e>f)and(f>g)and(g>h) and (a>e)and(b>f) and (c>g)and(d>h)): print(a,b,c,d) print(e,f,g,h) count=count+1 print(count)
运行结果
编程思想:
a | b | c | d |
---|---|---|---|
e | f | g | h |
每一个字母表示都有8种可能所以所以8层循环,遍历出所有结果:
for a in range(1,9): for b in range(1, 9): for c in range(1, 9): for d in range(1, 9): for e in range(1, 9): for f in range(1, 9): for g in range(1, 9): for h in range(1, 9):
但是每一个字母表示的值并不相同,换句话说,如果a取1的话,那么b,c,d,e,f,g,h都不能取1。后面的取值一样。
if (a!=b and a!=c and a!=d and a!=e and a!=f and a!=g and a!=h and b!=c and b!=d and b!=e and b!=f and b!=g and b!=h and c != d and c != e and c != f and c != g and c != h and d != e and d != f and d != g and d != h and e != f and e != g and e != h and f != g and f != h and g!=f ):
筛选出左边数值大于右边,上边大于下边
(a>b) and (b>c) and (c>d)and(e>f)and(f>g)and(g>h) #左边大于右边 and (a>e)and(b>f) and (c>g)and(d>h)#上边大于下边
这篇关于python编程比赛--将1-8几个数字填写下边表格,要求每一列数字比右边大,每一行数字比下边大,有几种填法?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享
- 2024-12-19使用python 把docx转为pdf文件有哪些方法?-icode9专业技术文章分享
- 2024-12-19python怎么更换换pip的源镜像?-icode9专业技术文章分享