如题所述
第1个回答 2016-01-22
设一分硬币个数为yiCount,两分硬币个数为erCount,五分硬币个数为wuCount.
则三个变量的活动区间为:
0 <= yiCount <= 10
0 <=erCount<= 5
0 <=wuCount<= 2
排列组合穷举这三个数在活动区间内的各种排列方式,当满足如下表达式时输出此组合:
yiCount * 1 + erCount * 2 + wuCount * 5 == 10代码:
<?php
for($yiCount = 0; $yiCount <= 10; $yiCount++){ //一分硬币最多十个
for ($erCount = 0; $erCount <= 5; $erCount++){ //两分硬币最多五个
for ($wuCount = 0; $wuCount <= 2; $wuCount++){ //五分硬币最多两个
if ($yiCount * 1 + $erCount * 2 + $wuCount * 5 == 10){
echo "{$yiCount}* 1 + {$erCount}* 2 + {$wuCount} * 5 = 10\r\n";
}
}
}
}
?>
执行结果:
0* 1 + 0* 2 + 2 * 5 = 10
0* 1 + 5* 2 + 0 * 5 = 10
1* 1 + 2* 2 + 1 * 5 = 10
2* 1 + 4* 2 + 0 * 5 = 10
3* 1 + 1* 2 + 1 * 5 = 10
4* 1 + 3* 2 + 0 * 5 = 10
5* 1 + 0* 2 + 1 * 5 = 10
6* 1 + 2* 2 + 0 * 5 = 10
8* 1 + 1* 2 + 0 * 5 = 10
10* 1 + 0* 2 + 0 * 5 = 10本回答被提问者采纳
则三个变量的活动区间为:
0 <= yiCount <= 10
0 <=erCount<= 5
0 <=wuCount<= 2
排列组合穷举这三个数在活动区间内的各种排列方式,当满足如下表达式时输出此组合:
yiCount * 1 + erCount * 2 + wuCount * 5 == 10代码:
<?php
for($yiCount = 0; $yiCount <= 10; $yiCount++){ //一分硬币最多十个
for ($erCount = 0; $erCount <= 5; $erCount++){ //两分硬币最多五个
for ($wuCount = 0; $wuCount <= 2; $wuCount++){ //五分硬币最多两个
if ($yiCount * 1 + $erCount * 2 + $wuCount * 5 == 10){
echo "{$yiCount}* 1 + {$erCount}* 2 + {$wuCount} * 5 = 10\r\n";
}
}
}
}
?>
执行结果:
0* 1 + 0* 2 + 2 * 5 = 10
0* 1 + 5* 2 + 0 * 5 = 10
1* 1 + 2* 2 + 1 * 5 = 10
2* 1 + 4* 2 + 0 * 5 = 10
3* 1 + 1* 2 + 1 * 5 = 10
4* 1 + 3* 2 + 0 * 5 = 10
5* 1 + 0* 2 + 1 * 5 = 10
6* 1 + 2* 2 + 0 * 5 = 10
8* 1 + 1* 2 + 0 * 5 = 10
10* 1 + 0* 2 + 0 * 5 = 10本回答被提问者采纳