Last active
January 25, 2017 01:03
-
-
Save nabeen/23fd621d3630bac111bdd5bfc8d3aa89 to your computer and use it in GitHub Desktop.
数学パズルQ3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 1-100までのカードがある | |
// n番目のカードからn-1枚おきにカードを裏返す | |
// 状態が変わらなくなるまで繰り返した時、裏になっているカードを求める | |
// 表をtrue、裏をfalseとして配列を生成 | |
$card_list = array_fill(1, 100, false); | |
// 最大値は最後の100枚目 | |
for ($i = 2; $i <= 100; $i++) { | |
// TODO: loopが多いので修正 | |
foreach ($card_list as $key => $val) { | |
if ($key % $i === 0) { | |
// 反転 | |
$card_list[$key] = !$card_list[$key]; | |
} | |
} | |
} | |
echo "reverse card list:".PHP_EOL; | |
// TODO: 不要なループなので修正 | |
foreach($card_list as $key => $val) { | |
if ($val === false) { | |
echo $key." "; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment