博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
九宫格,每行每列及对角之和是15
阅读量:4343 次
发布时间:2019-06-07

本文共 1256 字,大约阅读时间需要 4 分钟。

1至9九个数字,横竖都有3个格,思考怎么使每行、每列和对角线上的三数之和都等于15

# !/usr/bin/env python# -*- coding:utf-8 -*-# Author:Hiuhung Wanlist0 = []  # 除了中间那个数是5,周围的是别的数for i in range(1, 10):    if i == 5:        continue    list0.append(i)'''假设第一个数是x,第二个是y,那么这9个数应该是:x	        y	    15-x-y20-2x-y	    5	    2x+y-10x+y-5	    10-y	10-x    '''count = 0for x in list0:    list_temp = list0[:]  # 复制    list_temp.remove(x)   # 去除x后的列表    for y in list_temp:        set0 = set([x, y, 15 - x - y, 20 - 2 * x - y, 5, 2 * x + y - 10, x + y - 5, 10 - y, 10 - x])        if (15-x-y) in list0 and (20-2*x-y) in list0 and (2*x+y-10) in list0 and (x+y-5) in list0 and (10-y) in list0 and (10-x) in list0 and len(set0) == 9:            count += 1            print("第%s种情况:x = %s, y = %s" % (count, x, y))            print(x, y, 15 - x - y)            print(20 - 2 * x - y, 5, 2 * x + y - 10)            print(x + y - 5, 10 - y, 10 - x)

  

运行结果如下:

第1种情况:x = 2, y = 72 7 69 5 14 3 8第2种情况:x = 2, y = 92 9 47 5 36 1 8第3种情况:x = 4, y = 34 3 89 5 12 7 6第4种情况:x = 4, y = 94 9 23 5 78 1 6第5种情况:x = 6, y = 16 1 87 5 32 9 4第6种情况:x = 6, y = 76 7 21 5 98 3 4第7种情况:x = 8, y = 18 1 63 5 74 9 2第8种情况:x = 8, y = 38 3 41 5 96 7 2Process finished with exit code 0

  

转载于:https://www.cnblogs.com/hiuhungwan/p/10539802.html

你可能感兴趣的文章
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>