Files
2026-10-09 12:33:29 +09:00

26 lines
645 B
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* 1, 2, 1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 1, 2, 3, …(以下続く)
*
* というような数列があります。
* この数列の50番目に出て来る数字を求めるプログラムを記述してください
*/
$base = 50;
$number = array();
$index = 0;
for ($i = 2; $index < $base; $i++) {
for ($j = 1; $j < $i && $index < $base; $j++, $index++) {
echo $j . ', ';
$number[$index] = $j;
}
for ($k = $i; $k > 1 && $index < $base; $k--, $index++) {
echo $k . ', ';
$number[$index] = $k;
}
}
echo '<br>';
echo '50番目に出てくる値は:' . $number[count($number) - 1];