Github地址:https://github.com/ZJW9633/hello-word/blob/master/Xingzhenke


题目分析:

10道题互相关联,耦合性强,暴力求解需4^10种可能,本人使用python的约束库constraint设置约束进行解决。

python代码:

 #-*- coding:utf-8 -*-
# 《2018刑侦科推理试题》非穷举的 Python 解法
# 需要先安装约束解决库 `pip install python-constraint`
# author:jinwei
# date:2018/04/1 from constraint import *
from time import time
problem = Problem() # a1 - a10 表示第一题到第十题的答案变量,答案使用“1”表示“A”, “2”表示“B”,以此类推
vars = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"]
problem.addVariables(vars, [1, 2, 3, 4]) #第 1 题
#略
#第 2 题
def a2_func(a2, a5):
return (a2 == 1 and a5 == 3) or (a2 == 2 and a5 == 4) or (a2 == 3 or a5 == 1) or (a2 == 4 or a5 == 2)
problem.addConstraint(a2_func, ["a2", "a5"]) #第 3 题
def a3_func(a2, a3, a4,a6):
return (a3 == 1 and a6 == a2 == a4 != a3) or (a3 == 2 and a3 == a2 == a4 != a6) \
or (a3 == 3 and a3 == a6 == a4 != a2) or (a3 == 4 and a3 == a6 == a2 != a4)
problem.addConstraint(a3_func, ['a2','a3','a4','a6']) #第 4 题
def a4_func(a1, a2, a4, a5, a6, a7, a9, a10):
return (a4 == 1 and a1 == a5) or (a4 == 2 and a2 == a7) or (a4 == 3 and a1 == a9) or (a4 == 4 and a6 == a10)
problem.addConstraint(a4_func, ['a1','a2','a4','a5','a6','a7','a9','a10']) #第 5 题
def a5_func( a4, a5, a7, a8, a9):
return (a5 == a8 == 1) or (a5 == a4 == 2) or (a5 == a9 == 3) or (a5 == a7 == 4)
problem.addConstraint(a5_func, ['a4','a5','a7','a8','a9']) #第 6 题
def a6_func(a1, a2, a3, a4, a5, a6 ,a8, a9, a10):
return (a6 == 1 and a2 == a4 == a8) or (a6 == 2 and a1 == a6 == a8) \
or (a6 == 3 and a3 == a10 == a8) or (a6 == 4 and a5 == a9 == a8)
problem.addConstraint(a6_func, ['a1','a2','a3','a4','a5','a6','a8','a9','a10']) #第 7 题
def a7_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
all_answers = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
counter = [0, 0, 0, 0]
for a in all_answers:
counter[a - 1] += 1
imin = counter.index(min(counter)) + 1
n = len(set(counter))
# 这题为“此10题种被选中的选项最少的为,ABCD选择,说明 ABCD 的数量个不相同”
return n == 4 and ((a7 == 1 and imin == 3) or (a7 == 2 and imin == 2) or (a7 == 3 and imin == 1) or (a7 == 4 and imin == 4))
problem.addConstraint(a7_func, vars) #第 8 题
def a8_func(a8, a7, a5, a2, a10, a1):
adj = lambda x: abs(a1 - x) != 1
return (a8 == 1 and adj(a7)) or (a8 == 2 and adj(a5)) or (a8 == 3 and adj(a2)) or (a8 == 4 and adj(a10))
problem.addConstraint(a8_func, ["a8", "a7", "a5", "a2", "a10", "a1"]) #第 9 题
def a9_func(a1, a2, a5, a6, a9, a10):
cond1 = a1 == a6
cond = lambda x: cond1 != (x == a5)
return (a9 == 1 and cond(a6)) or (a9 == 2 and cond(a10)) or (a9 == 3 and cond(a2)) or (a9 == 4 and cond(a9))
problem.addConstraint(a9_func, ["a1", "a2", "a5", "a6", "a9", "a10"]) #第 10 题
def a10_func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10):
all_answers = [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]
counter = [0, 0, 0, 0]
for a in all_answers:
counter[a - 1] += 1
v = abs(max(counter) - min(counter))
return (a10 == 1 and v == 3) or (a10 == 2 and v == 2) or (a10 == 3 and v == 4) or (a10 == 4 and v == 1)
problem.addConstraint(a10_func, vars) # 约束设置完毕,开始求解
start_time=time()
solutions = problem.getSolutions()
elsap_time=time()-start_time
print("解决时间:{0}s".format(elsap_time))
print("答案组数:{0}".format(len(solutions))) chars = ['A', 'B', 'C', 'D'] for si in range(0, len(solutions)):
print("\n---------- 第 {0} 组结果 ----------".format(si + 1))
s = solutions[si]
for i in range(1, 11):
key = "a" + str(i)
answer = chars[s[key] - 1]
print("{0}".format( answer),end='')

代码结果:

解决时间:0.5780596733093262s
答案组数:1 ---------- 第 1 组结果 ----------
BCACACDABA
Process finished with exit code 0

总结:

出现的问题:在进行约束条件的时,将传入所有参数a1~a10,并且将vars传入constraint中,增加了不必要的计算,大大降低了效率。

改进前运行所需时间:

改进:重新编写约束函数,只传入必要的参数,提高效率

改进后的运行时间:0.5s

python的约束库constraint解决《2018刑侦科题目》的更多相关文章

  1. 【python】对于程序员来说,2018刑侦科推理试卷是问题么?

    最近网上很火的2018刑侦科推理试卷,题目确实很考验人逻辑思维能力. 可是对于程序员来说,这根本不是问题.写个程序用穷举法计算一遍即可,太简单. import itertools class Solu ...

  2. 解决python的requests库在使用过代理后出现拒绝连接的问题

    在使用过代理后,调用python的requests库出现拒绝连接的异常 问题 在windows10环境下,在使用代理(VPN)后.如果在python中调用requests库来地址访问时,有时会出现这样 ...

  3. Python安装第三方库的安装技巧

    电脑:Windows10 64位. Python IDE 软件:JetBrains PyCharm Community Edition 2018.1.3 x64 Python version : Py ...

  4. python中requests库使用方法详解

    目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...

  5. Python常用的库简单介绍一下

    Python常用的库简单介绍一下fuzzywuzzy ,字符串模糊匹配. esmre ,正则表达式的加速器. colorama 主要用来给文本添加各种颜色,并且非常简单易用. Prettytable ...

  6. Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED

    Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描述 使用pip按照virtualenv报错,如下: pip install virtua ...

  7. 使用boost.python封装C++库

    使用boost.python封装C++库 C++以高性能著称,但是编写较为复杂.而简洁是Python的强项.如果能珠联璧合,就能发挥两家之长.本文尝试用boost库的python模块封装C++ 前期准 ...

  8. python 各种开源库

    测试开发 来源:https://www.jianshu.com/p/ea6f7fb69501 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. 链接 sel ...

  9. python爬虫---selenium库的用法

    python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...

随机推荐

  1. android官方技术文档翻译——Case 标签中的常量字段

    本文译自androd官方技术文档<Non-constant Fields in Case Labels>,原文地址:http://tools.android.com/tips/non-co ...

  2. html案例详解(一)

    一.入门. <html> <!-- 头信息的作用 1. 可以设置网页的标题. 2. 可以通知浏览使用指定的码表解释html页面. 3. --> <head> < ...

  3. android binder机制详解

    摘要 Binder是android中一个很重要且很复杂的概念,它在系统的整体运作中发挥着极其重要的作用,不过本文并不打算从深层次分析Binder机制,有两点原因:1是目前网上已经有2篇很好的文章了,2 ...

  4. saiku中文维度,补充说明

    saiku在筛选中文维度 会出现浏览器白屏 停止响应的现象,经过跟踪源代码,分析原来在linux 操作系统中 数据库读取的中文和界面选取的编码是不一致的 解决方法, classes\saiku-dat ...

  5. Oracle Applications DBA 基础(一)

    1.引子 2014年9月13日 20:33 <oracle Applications DBA 基础>介绍Oracle Applications R12的系统架构, 数据库后台及应用系统的基 ...

  6. 点击table中的某一个td,获得这个tr的所有数据

    功能: 点击table中的某一个td,获得这个tr的所有数据 效果图 <html> <head> <script> function getData2(elemen ...

  7. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  8. shell-like program(shell程序的基本实施部分)

    直接上代码: #include "apue.h" #include <sys/wait.h> int main(void) { char buf[MAXLINE]; / ...

  9. Android群英传笔记——第四章:ListView使用技巧

    Android群英传笔记--第四章:ListView使用技巧 最近也是比较迷茫,但是有一点点还是要坚持的,就是学习了,最近离职了,今天也是继续温习第四章ListView,也拖了其实也挺久的了,list ...

  10. SharePoint 2013 页面访问,Url中间多一段&quot;_layouts/15/start.aspx#&quot;

    问题描述: 我想访问如下页面 http://Host/_layouts/15/ManageFeatures.aspx 点击以后页面地址没有错,但是中间多了一段"_layouts/15/sta ...