Advertisement
ttxnam

DuongCheo-php

Oct 7th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author ttxnam
  4.  * @copyright 2012
  5.  */
  6.  
  7. $n = 15; // Tổng số dòng
  8. $arr = array(array()); // Khai báo mảng
  9.  
  10. // Gán giá trị
  11. for($row = 0; $row < $n; $row++)
  12. {
  13.     for($col = 0; $col < $n; $col++)
  14.     {
  15.         // Gán từ trái sang phải, giá trị tăng dần theo từng dòng
  16.         if($col == $row && $row <= ($n-1)/2)
  17.         {
  18.             if(($n-1-$col) != $row)
  19.             {
  20.                 $arr[$row][$n-1-$col] = $row + 1;
  21.             }
  22.             $arr[$row][$col] = $row + 1;
  23.         }
  24.         // Gán từ trái sang phải, giá trị giảm dần theo từng dòng
  25.         if($col == $row && $row > ($n-1)/2)
  26.         {
  27.             $arr[$row][$col] = $n - $row;
  28.             $arr[$row][$n-1-$row] = $n - $row;
  29.         }
  30.     }  
  31. }
  32.  
  33. // In kết quả
  34. for($row = 0; $row < $n; $row++)
  35. {
  36.     for($col = 0; $col < $n; $col++)
  37.     {      
  38.         if(!empty($arr[$row][$col]))
  39.             echo $arr[$row][$col];
  40.         else
  41.             echo '-';
  42.     }
  43.     echo "<br/>";
  44. }
  45.  
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement