示例1:@H_502_1@
代码如下:
502_1@PHP@H_502_1@for($q=1;$q<=9;$q++){@H_502_1@ for($w=0;$w<=9;$w++){@H_502_1@ for($e=0;$e<=9;$e++){@H_502_1@ if($q*$q*$q + $w*$w*$w + $e*$e*$e ==@H_502_1@ 100*$q + 10*$w + $e){@H_502_1@ echo "$q $w $e "."
";@H_502_1@ }@H_502_1@ }@H_502_1@ }@H_502_1@}@H_502_1@?>@H_502_1@
示例2:
代码如下:
502_1@PHP@H_502_1@function cube( $n )@H_502_1@{@H_502_1@ return $n * $n * $n;@H_502_1@}function is_narcissistic ( $n )@H_502_1@{@H_502_1@ $hundreds = floor( $n / 100); //分解出百位@H_502_1@ $tens = floor( $n / 10 ) % 10; //分解出十位@H_502_1@ $ones = floor( $n % 10 ); //分解出个位@H_502_1@ return (bool)(cube($hundreds)+cube($tens)+cube($ones) == $n);@H_502_1@}@H_502_1@for ( $i = 100; $i < 1000; ++ $i )@H_502_1@{@H_502_1@ if ( is_narcissistic($i) )@H_502_1@ echo $i."\n";@H_502_1@}@H_502_1@?>@H_502_1@
示例3:@H_502_1@
代码如下:
502_1@PHP@H_502_1@//阿姆斯特朗数:一个k位数,它的每个位上的数字的k次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)@H_502_1@class Armh3 {@H_502_1@static function index(){@H_502_1@ for ( $i = 100; $i < 100000; $i++ ) {@H_502_1@ echo self::is_armh3($i) ? $i . '
' : '';@H_502_1@ }@H_502_1@}@H_502_1@static function is_armh3($num){@H_502_1@ $s = 0;@H_502_1@ $k = strlen($num);@H_502_1@ $d = str_split($num);@H_502_1@ foreach ($d as $r) {@H_502_1@ $s += bcpow($r,$k);@H_502_1@ }@H_502_1@ return $num == $s;@H_502_1@}@H_502_1@}@H_502_1@Armh3::index();@H_502_1@
' : '';@H_502_1@ }@H_502_1@}@H_502_1@static function is_armh3($num){@H_502_1@ $s = 0;@H_502_1@ $k = strlen($num);@H_502_1@ $d = str_split($num);@H_502_1@ foreach ($d as $r) {@H_502_1@ $s += bcpow($r,$k);@H_502_1@ }@H_502_1@ return $num == $s;@H_502_1@}@H_502_1@}@H_502_1@Armh3::index();@H_502_1@
示例4:
代码如下:
502_1@
@H_502_1@ @H_502_1@
function winter($num)@H_502_1@{@H_502_1@ if($num<1000){@H_502_1@ //定义个位@H_502_1@ $ge=$num%10;@H_502_1@ //定义十位@H_502_1@ $ten=(($num%100)-$ge) /10;@H_502_1@ //定义百位@H_502_1@ /*floor取整,忽略小数点后面的所有数*/@H_502_1@ $hundred=floor($num/100);@H_502_1@ $sum1=$ge*$ge*$ge+$ten*$ten*$ten+$hundred*$hundred*$hundred;@H_502_1@ if($sum1==$num){@H_502_1@ return 1;@H_502_1@ } else{@H_502_1@ return 0;@H_502_1@ }
} else{@H_502_1@ return -1;@H_502_1@ }@H_502_1@ }
if(winter(371)==-1) {@H_502_1@ echo "大于1000的数";@H_502_1@ }else{@H_502_1@ if(winter(371)) {@H_502_1@ echo "Yes";@H_502_1@ } @H_502_1@ else{@H_502_1@echo "No";@H_502_1@}@H_502_1@ }
?>