1 ";14 return;15 }16 17 if ($sum < $n) {18 combination($sum, $sum, $comb); #sum < n,则不可能需要比sum大的数来构成组合19 return;20 }21 22 #结果求得23 if ($sum == 0) {24 #输出元素组合25 echo "Combination: ";26 foreach ($comb as $val) {27 echo $val . " ";28 }29 echo "";30 return;31 }32 33 #组合里包含n的情况34 $comb[] = $n;35 combination($sum - $n, $n - 1, $comb);36 37 #组合里不包含n的情况38 array_pop($comb);39 combination($sum, $n - 1, $comb);40 }41 42 combination(10, 15, array());43 ?>