-
Notifications
You must be signed in to change notification settings - Fork 1
/
hbh.php
51 lines (44 loc) · 1.09 KB
/
hbh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
//案例: 有红、白、黑三种球若干个,其中红、白球共25个,白、黑球共31个,红、黑球共28个,
//求这三种球各多少个?
//请同时计算出使用了多少次循环——找出最少的循环次数
//用于记录循环了多少次(才找出答案)
$count = 0;
for ($hong=1;$hong<=25;$hong++) {
for ($bai=1; $bai <= 25; $bai++) {
for ($hei=1; $hei <= 28 ; $hei++) {
$count ++ ;
if($hong+$bai == 25 && $bai + $hei == 31 && $hong + $hei == 28){
echo "$hong,$bai,$hei";
}
}
}
}
echo "循环$count";
echo "<h3>优化1</h3>";
$count=0;
for ($hong=1;$hong<=25;$hong++) {
for ($bai=1; $bai <= 25; $bai++) {
for ($hei=1; $hei <= 28 ; $hei++) {
$count ++ ;
if($hong+$bai == 25 && $bai + $hei == 31 && $hong + $hei == 28){
echo "$hong,$bai,$hei";
break 3;
}
}
}
}
echo "循环$count";
echo "<h3>优化2</h3>";
$count = 0;
for ($hong=1; $hong <= 25; $hong++) {
$bai = 25 - $hong;
$hei = 28 - $hong;
$count ++ ;
if($bai + $hei == 31){
echo "红球$hong ,白球$bai, 黑球$hei";
break;
}
}
echo "循环$count";
?>