1 import java.util.Scanner; 2 3 public class Main { 4 5 static int n,count,T; 6 static int[] a = new int[22]; 7 static int[] b = new int[22]; 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 11 Scanner sc = new Scanner(System.in); 12 n =sc.nextInt(); 13 14 for(int i =0;i<n;i++) 15 { 16 a[i]=sc.nextInt(); 17 } 18 T=sc.nextInt(); 19 20 count = 0; 21 findAns(n-1,0,0); 22 System.out.println(count); 23 24 25 } 26 27 static void findAns(int aIndex, int bIndex, int sum) 28 { 29 if(aIndex < 0){ 30 return; 31 } 32 33 34 findAns(aIndex-1, bIndex, sum); 35 b[bIndex] = a[aIndex]; 36 sum += a[aIndex]; 37 if(sum == T){ 38 count ++; 39 print(bIndex); 40 } 41 findAns(aIndex-1, bIndex+1, sum); 42 } 43 44 static void print(int bIndex) 45 { 46 int i; 47 48 for(i=bIndex; i>=0; i--){ 49 System.out.print(b[i]+" "); 50 } 51 System.out.println(); 52 53 } 54 55 }
原文:http://www.cnblogs.com/fjl-vxee/p/6657573.html