HKMUD 香港群俠傳

https://web.hkmud.com

使用者工具

網站工具


中國餘數定理

差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

Both sides previous revision 前次修改
中國餘數定理 [2020/01/22 05:44]
reinhard 移除
— (目前版本)
行 1: 行 1:
-<​HTML>​ +
- 問題:<​input id="​question"​ type="​text"​ value="​這個數,它除5余2,它除25余17,它除57余32,它除183余56"​ placeholder="​它除A余B,它除C余D,它除E余F,它除G余H"​ size="​60"/><​br/>​ +
- 指令:<​input id="​answer"​ type="​text"​ size="​60"​ readonly/>​ +
- <script type="​text/​javascript">​ +
- const REGEX_QUESTION = /​它除(\d+)余(\d+)/​g;​ +
- const input = document.getElementById('​question'​);​ +
- const output = document.getElementById('​answer'​);​ +
- let readQuestion = () => { +
- let question = input.value;​ +
- let match; +
- let pairs = []; +
- while (match=REGEX_QUESTION.exec(question)) { +
- pairs.push({divisor:​ parseInt(match[1],​ 10), reminder: parseInt(match[2],​ 10)}); +
-+
- if (pairs.length) { +
- let answers = solve(pairs);​ +
- if (answers.length) { +
- output.value = `do ${answers.map(a => `turn ${a}`).join(','​)}`;​ +
- output.focus();​ +
- document.execCommand('​copy'​);​ +
- return;​ +
-+
- else { +
- output.value = '???';​ +
-+
-+
- else { +
- output.value = '';​ +
-+
- }; +
- let solve = pairs => { +
- let inc = 0; +
- let offset = 0; +
- for (let i=0; i<​pairs.length;​ i++) { +
- if (pairs[i].divisor > inc) { +
- inc = pairs[i].divisor;​ +
- offset = pairs[i].reminder;​ +
-+
-+
- let answers = []; +
- for (let num=offset; num<​10000;​ num+=inc) { +
- if (pairs.every(pair => num % pair.divisor == pair.reminder)) { +
- answers.push(num);​ +
-+
-+
- return answers; +
-+
- input.addEventListener('​paste',​ readQuestion,​ false); +
- input.addEventListener('​change',​ readQuestion,​ false); +
- input.addEventListener('​blur',​ readQuestion,​ false); +
- readQuestion();​ +
- </​script>​ +
-</​HTML>​+