小明参加X星球的打靶比赛。
比赛使用电子感应计分系统。其中有一局,小明得了96分。
这局小明共打了6发子弹,没有脱靶。
但望远镜看过去,只有3个弹孔。
显然,有些子弹准确地穿过了前边的弹孔。
不同环数得分是这样设置的:
1,2,3,5,10,20,25,50
那么小明的6发子弹得分都是多少呢?有哪些可能情况呢?
下面的程序解决了这个问题。
仔细阅读分析代码,填写划线部分缺失的内容。
结果:i!=0?ho-1:ho
ho代表单孔剩余数目
k代表当前是第几环,
i代表当前环射了几次
da[]存储每环设了几次
当i=0表示没射中当前环,剩余弹孔数目不变,反之弹孔数-1
1 public class Main 2 { 3 static void f(int[] ta, int[] da, int k, int ho, int bu, int sc) 4 { 5 if(ho<0 || bu<0 || sc<0) return; 6 if(k==ta.length){ 7 if(ho>0 || bu>0 || sc>0) return; 8 for(int i=0; i<da.length; i++){ 9 for(int j=0; j<da[i]; j++) 10 System.out.print(ta[i] + " "); 11 } 12 System.out.println(); 13 return; 14 } 15 16 for(int i=0; i<=bu; i++){ 17 da[k] = i; 18 f(ta, da, k+1, i!=0?ho-1:ho , bu-i, sc-ta[k]*i); // 填空位置 19 } 20 21 da[k] = 0; 22 } 23 24 public static void main(String[] args) 25 { 26 int[] ta = {1,2,3,5,10,20,25,50}; 27 int[] da = new int[8]; 28 f(ta, da, 0, 3, 6, 96); 29 } 30 }