Help us understand the problem. What is going on with this article?

forループの練習問題(数字でピラミッドを作る)  上級・解答編

More than 3 years have passed since last update.

問題

下記のリンクの練習問題の解答(PHPで記述)になります。
http://qiita.com/takuya-kodama/items/5a1dd8da56e7a87ec58f

解答よりも良いコードがありましたらコメント等で教えていただければと思います!

解答

answer.php
<?php

#問題1
for($a = 1 ; $a <= 3; $a++){
  for($b = 4; $b >= $a+1 ; $b--){
    echo $b-$a;
  }
  echo "<br />";
}

#問題2
for($a = 3 ; $a >= 1 ; $a --){
  for($c = 2 ; $c >= $a ; $c --){
    echo "*";
  }

  for($b = 1 ; $b <= $a ; $b ++){
    echo $b;
  }
  echo "<br />";
}

#問題3
$x = 6;
for($a =1 ; $a <= 3 ; $a ++){
  for($b = 1 ; $b<= $a ; $b++){
    echo $x;
    $x --;
  }
  echo "<br />";
}

#問題4
for($a = 4 ; $a >= 1 ; $a--){
  for($b = 3 ; $b >= $a ; $b--){
    echo "*";
  }
  for($c = 1 ; $c <=$a ; $c++){
    echo $c;
  }
  for($d = $c-2 ; $d >=1  ; $d --){
    echo $d;
  }
  echo "<br />";
}


#問題5
for($a = 1 ; $a <= 9 ; $a ++){
  if($a <= 5){
    for($b = 4 ; $b >= $a ; $b --){
      echo "*";
    }
    for($c = 1; $c <= $a ; $c++){
      echo $c;
    }
    for($d =$c -2 ; $d >= 1 ; $d--){
      echo $d;
    }
    echo "<br />";
  }
  else if($a >=6 ){
    for($e = 6 ; $e <= $a ; $e++){
      echo "*";
    }
    for($f = 1 ; $f <=10-$a  ; $f++){
      echo $f;
    }
    for($g = $f-2 ; $g >=1  ; $g --){
      echo $g;
    }
  echo "<br />";
  }
}

#問題6
for($a = 5 ; $a >= 1 ; $a --){
  for($b = 2 ; $b <= $a; $b ++){
    echo "*";
  }
  for($c = 5 ; $c >= $a ;$c --){
    echo $c;
  }
  for($d = $c+2 ; $d <= 5   ; $d ++){
    echo $d;
  }
  echo "<br />";
}


?>

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした