Designer algorithme pona koluka combinaison ya dice tango bapesi somme cible.
Algorithme moko possible mpo na koluka combinaison ya ba rouleaux ya dice oyo ebakisaka na somme cible ezali kosalela fonction récursive. Tala ndakisa ya ndenge algorithme ekokaki kosala : 1. Limbola fonction, `dice_combinations(target_sum, dice, current_sum, current_combination, all_combinations)`, esika `target_sum` ezali somme oyo olingi, `dice` ezali array oyo ezali komonisa ba valeurs possibles ya rulo ya die (ndakisa, `[1, 2, 3, 4, 5, 6]` mpo na die ya mbala na mbala ya ngambo motoba), `current_sum` ezali somme ya lelo ya ba rouleaux ya dice tii sikoyo, `current_combination` ezali an array oyo ezali komonisa bosangani ya lelo ya ba rouleaux ya dice oyo ezali kotalelama, mpe `all_combinations` ezali ensemble ya bosangani nyonso ya ba rouleaux ya dice oyo ebakisami na somme cible. 2. Soki `current_sum` ekokani na `target_sum`, bakisa `current_combination` ya ba rouleaux ya dice na array ya `all_combinations`. 3. Soki `current_sum` ezali monene koleka `target_sum`, zonga kozanga kosala eloko, lokola ezali na ntina te ya kokoba na nse ya etape oyo ya récursion lisusu. 4. Soki te, mpo na motuya moko na moko oyo ekoki kosalema na kati ya tableau `dice`, benga fonction `dice_combinations` na ndenge ya récursive, kosala mikolo oyo `current_sum` na kobakisa motuya ya rouleau ya die mpe kobakisa motuya na `current_combination`. 5. Zongisa array ya `ba combinaisons_nionso`. Ndakisa ya ndenge nini algorithme oyo ekokaki kosalelama na Python ezali boye: ```python def dice_combinations(target_sum, dice, current_sum=0, current_combination=[], all_combinations=[]): soki current_sum == target_sum: nionso_combinations.append (combination_current) zongisa soki somme_ya lelo > somme_cible: zonga pona die na dice: dice_combinations( somme_cible, dice, somme_current + die, combinaison_current + [kufa], combinaisons_nionso ) zongisa ba combinaisons_nionso nionso_combinations = dice_combinations(9, [1, 2, 3, 4, 5, 6]) mpo na bosangani na bosangisi_nyonso: print(bosangisi) ``` Yango ekozongisa bobimisi oyo elandi: ``` [1, 1, 1, 1, 1, 1, 1, 1, 1