php实现水仙花数的4个示例分享
php  /  管理员 发布于 7年前   527
示例1: "; 示例2: function is_narcissistic ( $n ) 示例3: 示例4: function winter($num) } else{ if(winter(371)==-1) { ?> 122 在 123 在 原梓番博客 在 博主 在 1111 在
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 "."
}
}
}
}
?>
function cube( $n )
{
return $n * $n * $n;
}
{
$hundreds = floor( $n / 100); //分解出百位
$tens = floor( $n / 10 ) % 10; //分解出十位
$ones = floor( $n % 10 ); //分解出个位
return (bool)(cube($hundreds)+cube($tens)+cube($ones) == $n);
}
for ( $i = 100; $i < 1000; ++ $i )
{
if ( is_narcissistic($i) )
echo $i."\n";
}
?>
//阿姆斯特朗数:一个k位数,它的每个位上的数字的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 . '
' : '';
}
}
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();
{
if($num<1000){
//定义个位
$ge=$num%10;
//定义十位
$ten=(($num%100)-$ge) /10;
//定义百位
/*floor取整,忽略小数点后面的所有数*/
$hundred=floor($num/100);
$sum1=$ge*$ge*$ge+$ten*$ten*$ten+$hundred*$hundred*$hundred;
if($sum1==$num){
return 1;
} else{
return 0;
}
return -1;
}
}
echo "大于1000的数";
}else{
if(winter(371)) {
echo "Yes";
}
else{
echo "No";
}
}您可能感兴趣的文章:
上一条:
C#使用PHP服务端的Web Service通信实例
下一条:
php命令行使用方法和命令行参数说明
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号