jackie 首页 我的空间 帮助 波罗蜜

吸血鬼数字算法PHP版

2008-05-13 13:44
Java编程思想中的小题目,用PHP写了一下子。
转载注明:http://u.boolome.com
<?php
$result = array();
 for ($m=1 ;$m<10 ;$m++){
 for($n =0 ;$n<10;$n++){
  for($x =1 ;$x<10;$x++){
   for($y =0 ;$y<10;$y++){
    $z = ($m*10+$n)*($x*10+$y);
    if($z <=1000||$z>9999){
     continue;
    }
    $tz = (string)$z;
    $tm = (string)$m;
    $tn = (string)$n;
    $tx = (string)$x;
    $ty = (string)$y;
    if(!(strpos($tz,$tm) === false) &&!(strpos($tz,$tn) === false)
      &&!(strpos($tz,$tx) === false)&&!(strpos($tz,$ty) === false) 
      ){
     for($j=0;$j<strlen($tz);$j++){
      $tmp=$tm.$tn.$tx.$ty;
      if(strpos($tmp,$tz{$j})===false){
       break 2;
      }
     }
     $result[$m.$n."x".$x.$y] = $z;
       
    }
   }
  }  
 }
 }
print("<pre>");
print_r(array_unique($result));
print("</pre>");
?>
结果为:
Array
(
    [15x93] => 1395
    [21x60] => 1260
    [21x87] => 1827
    [27x81] => 2187
    [30x51] => 1530
    [35x41] => 1435
    [75x77] => 5775
    [80x86] => 6880
    [98x91] => 8918
)
594 次阅读 | 1 个评论

留下脚印