1、求一个一维数组的最大值。
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 3 $a = array(0,5,10,2,6); 4 $max_1 = $a[0]; 5 for($i = 0;$i < count ($a);$i++) 6 { 7 if($max_1 <= $a[$i]) 8 $max_1 = $a[$i]; 9 } 10 echo $max_1;
截图:
2、求一个一维数组的元素之和。
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php $a = array(0,5,10,2,6); $sum = 0; for($i = 0;$i < count ($a);$i++) { $sum = $sum + $a[$i]; } echo $sum;
截图:
3、求一个数的阶乘。
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php $sum = 1; if(!empty($post)) { if($post > 0) { $n = $post; for($i = $n;$i > 0;$i++) { $sum *= n; } } else{ echo "ERROR"; } } ?> <!doctype html > <html lang = "en" > <head > <meta charset = "UTF-8" > <meta name = "viewport" content = "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" > <meta http - equiv = "X-UA-Compatible" content = "ie=edge" > <title >阶乘</title > </head > <body > <form action="" method="post"> <table border="1"> <tr> <td colspan="2"> 求阶乘 </td> </tr> <tr> <th> 请输入一个数 </th> <td> <input type="text" id="num" name="num"> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> <p>结果:</p> <p> <?php echo $sum; ?> </p> </form> </body > </html >
截图:
4、打印水仙花数。打印水仙花
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php for($a = 0;$a<=20;$a++) { for($b = 0;$b<=20;$b++) { for($c = 0;$c<=20;$c++) { $sum1 = $a*100 + $b*10 + $c; $sum2 = $a*$a*$a + $b*$b*$b +$c*$c*$c; if($sum1 == $sum2) echo '水花仙数为:', $a ,$b,$c, '</br>'; } } }
截图:
5、设计一个计算器。
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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的数
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php for($n = 1;$n <= 10000;$n++) { if($n % 3 == 0) { if(($n % 10) == 6) { echo $n,' '; } } }
截图:
7. 操场上100多人排队,3人一组多1人,4人一组多2人,5人一组多3人,共多少人?
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php for($n = 100; ;$n++) { if((($n%3) == 1) && (($n%4) == 2) && (($n%5)==3)) { echo $n; break; } }
截图:
8. 假设某人有100,000 现金。每经过一次路口需要进行一次交费。交费规则为当他现金大于50,000 时每次需要交5%如果现金小于等于50,000 时每次交5,000。请写一程序计算此人可以经过多少次这个路口。
源代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php $sum = 0; $n = 100000; while($n >= 5000) { if($n > 50000) { $n = $n - $n*0.05; $sum++; } else { $n -= 5000; $sum++; } } echo '此人可经过',$sum, '次';
截图: