no message
This commit is contained in:
25
Question06-for/Ex09.php
Normal file
25
Question06-for/Ex09.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?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];
|
||||
Reference in New Issue
Block a user