Skip to content

Instantly share code, notes, and snippets.

@ronnywang
Last active March 27, 2016 21:05
Show Gist options
  • Save ronnywang/ab9ac41e7690f4cb31d1 to your computer and use it in GitHub Desktop.
Save ronnywang/ab9ac41e7690f4cb31d1 to your computer and use it in GitHub Desktop.
<?php
/**
* $a 是遞迴到的答案
* $unused_pools 是還未使用的數字
*/
function guess_function($a, $unused_pools) {
// 找到九位的話,驗證下面的算式是否成立 (EF + GH = PPP)
if (count($a) == 9) {
if (($a[4] * 10 + $a[5]) + ($a[6] * 10 + $a[7]) != (100 * $a[8] + 10 * $a[8] + $a[8])) {
return;
}
echo "答案: {$a[0]}{$a[1]} - {$a[2]}{$a[3]} = {$a[4]}{$a[5]} + {$a[6]}{$a[7]} = {$a[8]}{$a[8]}{$a[8]}\n";
return;
}
// 找到六位時,驗證上面的算式是否成立 (AB - CD = EF)
if (count($a) == 6) {
if (($a[0] * 10 + $a[1]) - ($a[2] * 10 + $a[3]) !== ($a[4] * 10 + $a[5])) {
return;
}
}
for ($i = 0; $i < count($unused_pools); $i ++) {
guess_function(
array_merge($a, array($unused_pools[$i])),
array_merge(array_slice($unused_pools, 0, $i), array_slice($unused_pools, $i + 1))
);
}
};
// 把 1 - 9 放出猜測清單
guess_function(array(), range(1, 9));
/* 結果:
答案: 85 - 46 = 39 + 72 = 111
答案: 86 - 54 = 32 + 79 = 111
答案: 95 - 27 = 68 + 43 = 111
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment