Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class FindCombinationsSumToN {
  5.  
  6. public void findCombinations(int n){
  7. System.out.println("N = " +n);
  8. List<Integer> combinationList = new ArrayList<>();
  9. combinationUtil(n, 0, 1, combinationList);
  10. }
  11.  
  12. public void combinationUtil(int n, int sum, int start, List<Integer> combinationList){
  13.  
  14. if(sum==n) {
  15. System.out.println(combinationList);
  16. return;
  17. }
  18.  
  19. for (int i = start; i <=9 ; i++) {
  20. combinationList.add(i);
  21. combinationUtil(n, sum + i, i + 1, combinationList);
  22. combinationList.remove(combinationList.size()-1);
  23. }
  24. }
  25.  
  26. public static void main(String[] args) {
  27. int n = 12;
  28. FindCombinationsSumToN c = new FindCombinationsSumToN();
  29. c.findCombinations(n);
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement