You cannot see this page without javascript.

Magic Square (마방진) - php source code

Programing 조회 수 1663 추천 수 0 2016.07.21 09:44:12

마방진 풀이법들을 종합하여 짠 PHP 소스를 개시한다.

다음의 코드는 input html 소스이다.

 

 <html>
<head>
<title>
</title>
</head>
<body>

<form action="magicsquare.php" method="post">
<p align="center"><table>
<tr>
<td align="center">
제 <input type="text" name="square" size="10" maxlength="10" />차 완전마방진 <input type="submit" value="만들기" />
</td>
</tr>
</table></p>
</form>


</body>
</html>

 

다음의 코드는 마방진 출력 php 코드이다.

 

<?php
$square=$_POST['square'];
function table()
{
 global $square;
 global $sq;
 $i = 0;
 $j = 0;
 $sum = 0 ;
 for ( $i=0 ; $i < $square ; $i++ )
 {
  $sum = $sum + $sq[$i][$square-$i-1] ;
 }
?>
 <table align="center" border="0" cellpadding="0" cellspacing="0" style="border: currentColor; border-image: none; font-family: &quot;맑은 고딕&quot;,sans-serif; font-size: 18px; border-collapse: collapse;">
  <tr>
   <td colspan="<?= $square ?>" align="center" style="border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid;">
    <?= $square ?>차
   </td>
   <td align="center" style="border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid;">
    <?= $sum ?>
   </td>
  </tr>
  <tr>
   <td rowspan="<?= $square ?>" colspan="<?= $square ?>" align="center" style="border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid;">
    <table width="100%" border="0" cellpadding="0" cellspacing="0" style="border: currentColor; border-image: none; font-family: &quot;맑은 고딕&quot;,sans-serif; font-size: 18px; border-collapse: collapse;">
<?php
 for ( $i=0 ; $i < $square ; $i++ )
 {
?>
     <tr>
<?php
  for ( $j=0 ; $j < $square ; $j++ )
  {
?>
      <td align="center" style="border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid;" width="<?= (100/$square) ?>%">
       <?= $sq[$i][$j] ?>
      </td>
<?php
  }
?>
     </tr>
<?php
 }
?>
    </table>
   </td>
<?php
 for ( $i=0 ; $i < $square ; $i++ )
 {
?>
   <td align="center" style="border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid;">
<?php
  $sum = 0 ;
  for ( $j=0 ; $j < $square ; $j++ )
  {
   $sum = $sum + $sq[$i][$j] ;
  }
?>
    <?= $sum ?>
   </td>
  </tr>
  <tr>
<?php
 }
 for ( $i=0 ; $i < $square ; $i++ )
 {
?>
   <td align="center" style="border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid;">
<?php
  $sum = 0 ;
  for ( $j=0 ; $j < $square ; $j++ )
  {
   $sum = $sum + $sq[$j][$i] ;
  }
?>
    <?= $sum ?>
   </td>
<?php
 }
?>
   <td align="center" style="border-top-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-bottom-color: rgb(0, 0, 0); border-left-color: rgb(0, 0, 0); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid;">
<?php
 $sum = 0 ;
 for ( $i=0 ; $i < $square ; $i++ )
 {
  $sum = $sum + $sq[$i][$i] ;
 }
?>
    <?= $sum ?>
   </td>
  <tr>
 </table>
<?php
}
if( $square <= 2 )
{
?>
 <p align="center">
  3이상의 숫자를 입력하여 주세요!
 </p>
<?php
}
else
{
?>
 <p align="center">
  제 <?= $square ?>차 완전마방진!<br />
  한 구역의 합은 <?= (($square*$square+1)*$square/2) ?>입니다.
 </p>
<?php
 if ( $square%2 == 1 )
 {
  $i = ($square-1)/2 + 1 ;
  $j = ($square-1)/2 ;
  for( ($k = 1) ; ($k <= ($square*$square)) ; $k++ )
  {
   $sq[$i][$j] = $k ;
   if ( $k % $square == 0 )
   {
    $i = $i + 2 ;
   }
   else
   {
    $i++ ;
    $j++ ;
   }
   if ( $i >= $square )
   {
    $i = $i - $square ;
   }
   elseif ( $j >= $square )
   {
    $j = $j - $square ;
   }
  }
 }
 elseif ( $square%4 == 0 )
 {
  $k = 1 ;
  for ( $i=0 ; $i < $square ; $i++ )
  {
   for ( $j=($square-1) ; $j >= 0 ; $j-- )
   {
    if ((($i%4 == 1) or ($i%4 == 2)) xor (($j%4 == 1) or ($j%4 == 2)))
    {
     $sq[$i][$j] = $square*$square-$k+1 ;
    }
    else
    {
     $sq[$i][$j] = $k ;
    }
    $k++ ;
   }
  }
 }
 else
 {
  $k = 1 ;
  for ( $i=0 ; $i < $square ; $i++ )
  {
   for ( $j=($square-1) ; $j >= 0 ; $j-- )
   {
    if (($i == $j ) or ($i+$j+1 == $square))
    {
     $sq[$i][$j] = $k ;
    }
    elseif (((($i+$j)%2 == 0) and ((($i+$j >= $square) and ($j < $square/2)) or (($i-$j > 0) and ($i < $square/2) and ($i > 2)) or (($j-$i > 0) and ($i >= $square/2)) or (($i+$j < $square) and ($j >= $square/2) and ($i > 1)))) or ((($i+$j)%2 == 1) and ((($i+$j < $square) and ($i >= $square/2)) or (($j-$i > 0) and ($j < $square/2) and ($i > 1)) or (($i-$j > 0) and ($j >= $square/2)) or (($i+$j > $square) and ($i < $square/2) and ($i > 2)))))
    {
     $sq[$i][$j] = $square*$square-$k+$square-$j*2 ;
    }
    elseif (((($i+$j)%2 == 0) and ((($j-$i > 0) and ($j < $square/2)) or (($i+$j >= $square) and ($i < $square/2) and ($j < $square-2)) or (($i+$j < $square) and ($i >= $square/2)) or (($i-$j > 0) and ($j >= $square/2) and ($j < $square-3)))) or ((($i+$j)%2 == 1) and ((($i-$j > 0) and ($i < $square/2)) or (($i+$j < $square) and ($j >= $square/2) and ($j < $square-3)) or (($i+$j >= $square) and ($j < $square/2)) or (($j-$i > 0) and ($i >= $square/2) and ($j < $square-2)))))
    {
     $sq[$i][$j] = ($i*2+1)*$square-$k+1 ;
    }
    else
    {
     $sq[$i][$j] = $square*$square-$k+1 ;
    }
    $k++ ;
   }
  }
 }
 table();
}
?>

 

 

태그
엮인글 :
List of Articles
번호 제목 글쓴이 날짜 조회 수
309 Xpress Engine xe 포인트 레벨과 등급 동기화 에드온 2.0 file LynX 2016-09-21 1277
308 Xpress Engine xe 현재접속과 로그인 수 표시 addon 수정본 file [1] LynX 2016-09-20 2238
307 Linux DDOS 방어 LynX 2016-09-19 456
306 Xpress Engine 현재접속과 로그인 수 표시 addon file LynX 2016-09-10 1476
305 Server 웹 동접자 수 확인 코드 LynX 2016-09-08 831
304 Linux 하위 디렉토리 문자열 일괄검색,치환 LynX 2016-08-24 250
303 Linux Apache httpd mod_evasive compile LynX 2016-08-22 245
302 Linux firewalld DDOS 차단 LynX 2016-08-22 953
301 Programing Magic Square (마방진) - 마방진코드와 나의 생각 03 LynX 2016-08-09 1564
300 Programing Magic Square (마방진) - 마방진코드와 나의 생각 02 LynX 2016-08-09 1552
299 Programing 베너관리 코드 LynX 2016-08-04 218
298 Programing Magic Square (마방진) - 마방진코드와 나의 생각 01 LynX 2016-07-27 1741
297 Linux iconv 이용 문자셋 일괄 변경 LynX 2016-07-21 330
» Programing Magic Square (마방진) - php source code LynX 2016-07-21 1663
295 Programing Magic Square (마방진) - java script source LynX 2016-07-18 2401
294 Programing Magic Square (마방진) - 4배수가 아닌 짝수 LynX 2016-07-15 2167
293 Programing Magic Square (마방진) - 홀수 LynX 2016-07-15 1677
292 Programing Magic Square (마방진) - 4의 배수 LynX 2016-07-15 1904
291 Linux OpenBSD APM Source Compile [2] LynX 2016-07-04 869
290 Network vpn LynX 2016-06-29 263

XE Login