wiki 上有这个游戏的标准表述 :珠玑妙算
用计算机穷举一遍就行了, 一共也就 种可能性.
有几位数字正确, 就是交集的元素个数
有几位位置正确, 就是向量差中0的个数.
一次猜测过程如图所示, 数字都正确, 位置都正确, 游戏结束:
all = Flatten [ Permutations /@ Subsets [ Range [ 0 , 9 ], { 4 }], 1 ]; callCC = { Length @ Intersection [ #1 , #2 ], Count [ #1 - #2 , 0 ]} & ; guess [ list_ ] := While [ True , Echo [ rnd = RandomChoice [ all ], "猜测: " ]; Echo [ ans = callCC [ rnd , list ], "判定: " ]; all = Select [ all , callCC [ rnd , # ] === ans & ]; If [ Length @ all == 1 , Print [ Text @ Style [ "游戏结束" , Red ]]; Return [ First @ all ], Echo [ Length @ all , "剩余可能性: " ]; ] ]