php打印水仙花数函数代码
例子,php打印水仙花数。
<html>
<head>
<title>php水仙花数函数--www.jbxue.com</title>
</head>
<body>
<?php
function winter($num)
{
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;
}
} else{
return -1;
}
}
if(winter(371)==-1) {
echo "大于1000的数";
}else{
if(winter(371)) {
echo "Yes";
} else{
echo "No";
}
}
?>
</body>
</html>
例2,php实现水仙花数
for($i=0;$i<1000;$i++)
{
$a=floor($i/100);//求出百位数
$b=floor($i/10)%10;//求出十位数
$c=$i%10;//求出个位数
//if($a*$a*$a+$b*$b*$b+$c*$c*$c==$i)
if(pow($a,3)+pow($b,3)+pow($c,3)==$i)//判断百位、十位
个位的立方和是否等于这个数本身
{
echo $i."<br>";
}
}
?>
您可能感兴趣的文章:
php水仙花数的小例子
php求三位数水仙花数
PHP求小于1000的水仙花数
php实现水仙花数代码分享
php打印水仙花数函数代码
Python水仙花数的编程代码如何写
python如何求水仙花数?
php实现水仙花数的4个例子
python输出水仙花数
php上传图片并打上透明水印的代码