1: switch不是好办法,但是题目要求的话就只能用了
$score = 55;
switch(floor($score/10)){
case 10: case 9: echo 'A';break;
case 8:echo 'B';break;
case 7:echo 'C';break;
case 6:echo 'D';break;
case 5: case 4: case 3: case 2: case 1: case 0: echo 'E';break;
}
2:
$max = 6;
$ret = 0;
for($i=1; $i<=$max; $i++){
$ret += array_product(range(1, $i));
}
echo $ret;
3:
$n = 100;
function sumExt($n){
return array_sum(range(1,$n));
}
echo sumExt($n);
4:最简单的逻辑是穷举法
$totalNum = 100;
$totalFee = 100;
$henFee = 3;
$chickenFee = 1/3;
for($henNum =0; $henNum<= 100; $henNum ++){
$chickenNum = $totalNum - $henNum;
if($totalFee == $henNum * $henFee + $chickenFee * $chickenNum){
echo '母鸡:',$henNum,'只;小鸡:',$chickenNum,'只',PHP_EOL;
}
}
码字不易,还望采纳
呵呵 LZ是用C的话就是for语句的活用了 迭代算法吧