Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author ttxnam
- * @copyright 2012
- */
- $n = 15; // Tổng số dòng
- $arr = array(array()); // Khai báo mảng
- // Gán giá trị
- for($row = 0; $row < $n; $row++)
- {
- for($col = 0; $col < $n; $col++)
- {
- // Gán từ trái sang phải, giá trị tăng dần theo từng dòng
- if($col == $row && $row <= ($n-1)/2)
- {
- if(($n-1-$col) != $row)
- {
- $arr[$row][$n-1-$col] = $row + 1;
- }
- $arr[$row][$col] = $row + 1;
- }
- // Gán từ trái sang phải, giá trị giảm dần theo từng dòng
- if($col == $row && $row > ($n-1)/2)
- {
- $arr[$row][$col] = $n - $row;
- $arr[$row][$n-1-$row] = $n - $row;
- }
- }
- }
- // In kết quả
- for($row = 0; $row < $n; $row++)
- {
- for($col = 0; $col < $n; $col++)
- {
- if(!empty($arr[$row][$col]))
- echo $arr[$row][$col];
- else
- echo '-';
- }
- echo "<br/>";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement