PHP实验二 PHP基本程序设计

1.一个一维数组的最大值

 

<?php
//求一个一维数组的最大值
$num=array(1,5,3,515,45,32,18);
$max=$num[0];
for($i=1;$i<count($num);$i++){
    if($num[$i]>$max){
        $max=$num[$i];
    }
}
echo '数组中最大的数为:'.$max;

 

 

 

2.一个一维数组的元素之和

 

<?php
//求一个一维数组的元素之和
$arr=array(4,6,2,22,11);
$sum=0;
for($i=0;$i<count($arr);$i++){
    $sum+=$arr[$i];
}
echo "数组元素之和: ".$sum;

 

 

 

3.求一个数的阶乘。界面如下图:

 

 

<?php
$result=0;
$n=0;
if(!empty($_POST))
{
    $result=1;
    $n=$_POST['num'];
    if($n<0)
    {
        $result=0;
    }
    if($n==0)
    {
        $result=1;
    }else{
        for($i=1;$i<=$n;$i++)
        {
            $result*=$i;
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>求阶乘</title>
</head>
<body>
<form action="" method="post">
    <table border="3" >
        <tr>

            <td colspan="2">
                求阶乘
            </td>
        </tr>
        <tr>
            <td>
                请输入一个数
            </td>
            <td>
                <input type="text" id="num" name="num" value=<?php echo $n;?>>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input  type="submit" value="提交">
            </td>
        </tr>
    </table>
    <?php echo $n,"的阶乘结果为:",$result; ?>
</form>
</body>
</html>

 

 

4.打印水仙花数。打印水仙花数

水仙花数的特点三位的数字,满足的条件是abc=a3+b3+c3

<?php
//计算立方
function cube($n)
{
    return $n*$n*$n;
}

//判断是否是水仙花数
function sxh($x)
{
    $hundred=floor($x/100);
    $ten=floor($x/10)%10;
    $one=floor($x%10);
    return (bool)($x==cube($hundred)+cube($ten)+cube($one));

}

//打印水仙花数
for($i=100;$i<1000;$i++) {
    if (sxh($i)) {
        echo $i . "\n";
    }
}

 

5.设计一个计算器。如下图所示:

 

 

<?php
$num1='';
$num2='';
$result="";
$operation='+';

if(!empty($_POST)){
    $num1=$_POST['num1'];
    $num2=$_POST['num2'];
    $operation=$_POST['operation'];
    if($operation=='+'){
        $result=$num1+$num2;
    }elseif($operation=='-'){
        $result=$num1-$num2;
    }elseif($operation=='*'){
        $result=$num1*$num2;
    }else{
        $result=$num1/$num2;
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算器</title>
</head>
<body>
<form action="" method="post">
    <label>第一个数:</label>
    <input type="text" name="num1" value=<?php echo $num1;?>>
    <br/>
    <input type="radio" name="operation" value="+" <?php if($operation=='+') echo 'checked'; ?>>+
    <input type="radio" name="operation" value="-" <?php if($operation=='-') echo 'checked'; ?>>-
    <input type="radio" name="operation" value="*" <?php if($operation=='*') echo 'checked'; ?>>*
    <input type="radio" name="operation" value="/" <?php if($operation=='/') echo 'checked'; ?>>/<br/>
    <label>第二个数:</label>
    <input type="text" name="num2" value=<?php echo $num2;?>><br/>
    结果为<input type="text" name="result" value=<?php echo $result;?>>
    <input type="submit" value ="计算">
</form>

</body>
</html>

 

 

6.能被3整除的个位数为6的数

 

<?php
//能被3整除的个位数为6的数(100以内的)  
for($i=0;$i<100;$i++)
{
    if($i%3==0&&$i%10==6)
    echo $i."   ";
}

 

 

 

7.操场上100多人排队,3人一组多1,4人一组多2,5人一组多3,共多少人?

 

<?php
//n%3==1  n%4==2  n%5==3
$n=0;
for($i=100;$i<200;$i++) {
    if ($i % 3 == 1 && $i % 4 == 2 && $i % 5 == 3) {
        $n = $i;
        echo "一共".$n."人        ";
    }

}

 

 

 

8.假设某人有100,000 现金。每经过一次路口需要进行一次交费。交费规则为当他现金大于50,000 时每次需要交5%如果现金小于等于50,000 时每次交5,000。请写一程序计算此人可以经过多少次这个路口。

 

<?php
 $n=0;
 
 //现金大于50000
 for($i=100000;$i>50000;$i-=$i*0.05)
        $n++;
//现金小于500000
for($j=$i;$j>=0&&$j<=50000;$j-=5000)
{
    $n++;
}  

echo "总共可以通过".$(n-1)."次这个路口";

 

posted @ 2020-05-10 16:14  祈欢  阅读(368)  评论(0编辑  收藏  举报