(HW)Combination(Java)

 1 public class test
 2 {
 3     public static void main(String[] args)
 4     {
 5         Vector<Integer> v = new Vector<>();
 6         for(int i = 1; i <= 5; i++)
 7             v.add(i);
 8         Scanner input = new Scanner(System.in);
 9         int k = input.nextInt();
10         int[] solution = new int[k];
11         System.out.println(Combination(v, solution, 0, 0));
12         input.close();
13     }    
14     
15     public static int Combination(Vector<Integer> v, int[] solution, int start, int pos)
16     {
17         if(pos == solution.length)
18         {
19             System.out.println(Arrays.toString(solution));
20             return 1;
21         }
22         
23         int count = 0;
24         for(int i = start; i < v.size(); i++)
25         {
26             solution[pos++] = v.get(i);
27             count += Combination(v, solution, i + 1, pos);
28             pos--;
29         }
30         
31         return count;
32     }
33 }

 

Guess you like

Origin www.cnblogs.com/Huayra/p/10973435.html