欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)水仙花數(shù)的4個(gè)示例分享

 更新時(shí)間:2014年04月08日 09:41:08   作者:  
水仙花數(shù)是指一個(gè) n 位數(shù) ( n≥3 ),它的每個(gè)位上的數(shù)字的 n 次冪之和等于它本身。(例如:1^3 + 3^3+ 5^3 = 153)這篇文章主要介紹了php實(shí)現(xiàn)水仙花數(shù)的4個(gè)示例分享,需要的朋友可以參考下

示例1:

復(fù)制代碼 代碼如下:

<?php
for($q=1;$q<=9;$q++){
    for($w=0;$w<=9;$w++){
      for($e=0;$e<=9;$e++){
        if($q*$q*$q + $w*$w*$w + $e*$e*$e ==
         100*$q + 10*$w + $e){
           echo "$q $w $e "."<p>";
        }
      }
    }
}
?>

示例2:

復(fù)制代碼 代碼如下:

<?php
function cube( $n )
{
    return $n * $n * $n;
}

function is_narcissistic ( $n )
{
    $hundreds = floor( $n / 100);    //分解出百位
    $tens = floor( $n / 10 ) % 10;    //分解出十位
    $ones = floor( $n % 10 );    //分解出個(gè)位
    return (bool)(cube($hundreds)+cube($tens)+cube($ones) == $n);
}

 
for ( $i = 100; $i < 1000; ++ $i )
{
    if ( is_narcissistic($i) )
        echo $i."\n";
}
?>

示例3:

復(fù)制代碼 代碼如下:

<?php
//阿姆斯特朗數(shù):一個(gè)k位數(shù),它的每個(gè)位上的數(shù)字的k次冪之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
class Armstrong {
 static function index(){
  for ( $i = 100; $i < 100000; $i++ ) {
   echo self::is_armstrong($i) ? $i . '<br>' : '';
  }
 }
 static function is_armstrong($num){
  $s = 0;
  $k = strlen($num);
  $d = str_split($num);
  foreach ($d as $r) {
   $s += bcpow($r, $k);
  }
  return $num == $s;
 }
}
Armstrong::index();

示例4:

復(fù)制代碼 代碼如下:

<html>

<head>
  <title></title>
</head>

<body>

<?php

 function winter($num)
 {
       if($num<1000){
       //定義個(gè)位
       $ge=$num%10;
       //定義十位
       $ten=(($num%100)-$ge) /10;
       //定義百位
       /*floor取整,忽略小數(shù)點(diǎn)后面的所有數(shù)*/
       $hundred=floor($num/100);
       $sum1=$ge*$ge*$ge+$ten*$ten*$ten+$hundred*$hundred*$hundred;
       if($sum1==$num){
               return 1;
                } else{
                        return 0;
                        }

               } else{
                       return -1;
                       }
         }

         if(winter(371)==-1) {
                 echo "大于1000的數(shù)";
            }else{
                  if(winter(371)) {
                          echo "Yes";
                          }
     else{
   echo "No";
   }
        }

?>

</body>
</html>

相關(guān)文章

最新評(píng)論