Skip to content

Instantly share code, notes, and snippets.

@makazeu
Last active March 9, 2018 01:11
Show Gist options
  • Select an option

  • Save makazeu/54394d837b68efc6f5ca3adb0a55b7e0 to your computer and use it in GitHub Desktop.

Select an option

Save makazeu/54394d837b68efc6f5ca3adb0a55b7e0 to your computer and use it in GitHub Desktop.
2018刑侦推理推理试题 —— Go语言
package main
import (
"fmt"
"time"
)
const QuestionNum = 10
var (
Choices = [4]byte{'A', 'B', 'C', 'D'}
answers = make([]byte, QuestionNum+1)
counts = make(map[byte]int)
checkers = []func(c byte) bool{
// question 1
func(c byte) bool {
return true
},
// question 2
func(c byte) bool {
choices := map[byte]byte{
'A': 'C',
'B': 'D',
'C': 'A',
'D': 'B',
}
return choices[c] == answers[5]
},
// question 3
func(c byte) bool {
choices := map[byte]byte{
'A': answers[3],
'B': answers[6],
'C': answers[2],
'D': answers[4],
}
sum := make(map[byte]int)
for _, v := range choices {
sum[v]++
}
return sum[choices[c]] == 1
},
// question 4
func(c byte) bool {
choices := map[byte]bool{
'A': answers[1] == answers[5],
'B': answers[2] == answers[7],
'C': answers[1] == answers[9],
'D': answers[6] == answers[10],
}
return choices[c]
},
// question 5
func(c byte) bool {
choices := map[byte]int{
'A': 8,
'B': 4,
'C': 9,
'D': 7,
}
return c == answers[choices[c]]
},
// question 6
func(c byte) bool {
choices := map[byte]byte{
'A': If(answers[2] == answers[4], answers[2], byte(0)).(byte),
'B': If(answers[1] == answers[6], answers[1], byte(0)).(byte),
'C': If(answers[3] == answers[10], answers[3], byte(0)).(byte),
'D': If(answers[5] == answers[9], answers[5], byte(0)).(byte),
}
return choices[c] == answers[8]
},
// question 7
func(c byte) bool {
choices := map[byte]byte{
'A': 'C',
'B': 'B',
'C': 'A',
'D': 'D',
}
var minN = QuestionNum + 1
for k, v := range counts {
if v < minN && choices[c] != k {
minN = v
}
}
return counts[choices[c]] < minN
},
// question 8
func(c byte) bool {
choices := map[byte]int{
'A': 7,
'B': 5,
'C': 2,
'D': 10,
}
return AbsInt(int(answers[1]), int(answers[choices[c]])) != 1
},
// question 9
func(c byte) bool {
choices := map[byte]int{
'A': 6,
'B': 10,
'C': 2,
'D': 9,
}
flag1 := answers[1] == answers[6]
flag2 := answers[choices[c]] == answers[5]
return flag1 != flag2
},
// question 10
func(c byte) bool {
choices := map[byte]int{
'A': 3,
'B': 2,
'C': 4,
'D': 1,
}
var minN = QuestionNum + 1
for _, v := range counts {
if v < minN {
minN = v
}
}
var maxN = 0
for _, v := range counts {
if v > maxN {
maxN = v
}
}
return choices[c] == maxN-minN
},
}
)
func dfs(depth int) {
if depth == QuestionNum+1 {
for i := 1; i <= QuestionNum; i++ {
if !checkers[i-1](answers[i]) {
return
}
}
output()
return
}
for _, c := range Choices {
answers[depth] = c
counts[c]++
dfs(depth + 1)
counts[c]--
}
}
func output() {
for i := 1; i <= QuestionNum; i++ {
fmt.Printf("%c ", answers[i])
}
fmt.Println()
//os.Exit(0)
}
func main() {
startTime := time.Now().UnixNano()
dfs(1)
endTime := time.Now().UnixNano()
fmt.Printf("time elapsed: %d ms\n", (endTime-startTime)/1000000)
}
func If(condition bool, trueVal, falseVal interface{}) interface{} {
if condition {
return trueVal
}
return falseVal
}
func AbsInt(x, y int) int {
return If(x < y, y-x, x-y).(int)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment