文章目录
凑算式
B DEF
A + — + ——— = 10
C GHI
这个算式中A-i代表1-9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。
分析
- 全排列yyds
运行代码
from itertools import permutations
res = 0
nums = [1,2,3,4,5,6,7,8,9]
for i in permutations(nums,9):
if i[0]+i[1]/i[2]+int(str(i[3])+str(i[4])+str(i[5]))/int(str(i[6])+str(i[7])+str(i[8])) == 10:
res+=1
print(res)
通过截图
方格填数
如上的10个格子填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
分析
- 对每个格子进行编号,然后把可以的条件加以判断即可,全排列还是yyds
运行代码
from itertools import permutations
res = 0
nums = [0,1,2,3,4,5,6,7,8,9]
for i in permutations(nums,10):
if i[0]+1 != i[1] and i[0]+1 != i[3] and i[0]+1 != i[4] and i[0]+1 != i[5]:
if i[1]+1 != i[0] and i[1]+1 != i[2] and i[1]+1 != i[4] and i[1]+1 != i[5] and i[1]+1 != i[6]:
if i[2]+1 != i[1] and i[2]+1 != i[5] and i[2]+1 != i[6]:
if i[3]+1 != i[0] and i[3]+1 != i[4] and i[3]+1 != i[7] and i[3]+1 != i[8]:
if i[4]+1 != i[0] and i[4]+1 != i[1] and i[4]+1 != i[3] and i[4]+1 != i[5] and i[4]+1 != i[7] and i[4]+1 != i[8] and i[4]+1 != i[9]:
if i[5]+1 != i[0] and i[5]+1 != i[1] and i[5]+1 != i[2] and i[5]+1 != i[4] and i[5]+1 != i[6] and i[5]+1 != i[8] and i[5]+1 != i[9]:
if i[6]+1 != i[1] and i[6]+1 != i[2] and i[6]+1 != i[5] and i[6]+1 != i[9]:
if i[7]+1 != i[3] and i[7]+1 != i[4] and i[7]+1 != i[8]:
if i[8]+1 != i[3] and i[8]+1 != i[4] and i[8]+1 != i[5] and i[8]+1 != i[7] and i[8]+1 != i[9]:
if i[9]+1 != i[4] and i[9]+1 != i[5] and i[9]+1 != i[6] and i[9]+1 != i[8]:
res+=1
print(res)
通过截图
答案是:1580
如有错误,敬请指正,欢迎交流,谢谢♪(・ω・)ノ