Guest User

Untitled

a guest
Apr 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. 6 + 13 = 19
  2. 13 + 3 + 3 = 19
  3.  
  4. Enter positive integers terminated with 0: 6 13 3 3 0
  5. Enter the desired sum: 19
  6. Solution 1:6 +13 =19
  7. Solution 2: 13 +3 +3 =19
  8. Found 2 solutions
  9.  
  10. public static boolean SubSetSum(int start, int[] nums, int target) {
  11. if (start >= nums.length) {
  12. return (target == 0);
  13. }
  14. if (SubSetSum(start + 1, nums, target - nums[start])) {
  15.  
  16. System.out.println( nums[start] );
  17. return true;
  18. }
  19. if (SubSetSum(start + 1, nums, target)) {
  20.  
  21. return true;
  22. }
  23. return false;
  24.  
  25. }
  26.  
  27.  
  28.  
  29.  
  30. public static void main(String[] args) {
  31.  
  32. int[] mySet = {4,1,3,2};
  33. int sum = 5;
  34. System.out.println("The Goal is : " + sum);
  35.  
  36. SubSetSum(0,mySet, sum) ;
  37. }
  38. }
  39.  
  40. import java.util.ArrayList;
  41. import java.util.Arrays;
  42. import java.util.HashSet;
  43. import java.util.List;
  44. import java.util.Set;
  45.  
  46. class ResursionTest {
  47. public static void findSubsets(Set<List<Integer>> allSubsets, List<Integer> nums) {
  48. if (nums.size() == 0) {
  49. return;
  50. }
  51.  
  52. // add the current list as a possibility
  53. allSubsets.add(new ArrayList<>(nums));
  54.  
  55. // then add a possibility that has one less
  56. for (int i = 0; i < nums.size(); i++) {
  57. final List<Integer> subset = new ArrayList<>(nums);
  58. subset.remove(i);
  59. findSubsets(allSubsets, subset);
  60. }
  61. }
  62.  
  63.  
  64. public static void main(String[] args) {
  65. final Integer[] array = {4, 1, 3, 2};
  66. final HashSet<List<Integer>> allSubsets = new HashSet<>();
  67.  
  68. findSubsets(allSubsets, Arrays.asList(array));
  69. System.out.println(allSubsets);
  70. }
  71. }
  72.  
  73. [[3, 2], [1], [4, 3], [2], [3], [1, 2], [4, 3, 2], [1, 3], [4], [4, 1, 2], [4, 1, 3], [4, 1, 3, 2], [4, 1], [1, 3, 2], [4, 2]]
  74.  
  75. import java.util.ArrayList;
  76. import java.util.Arrays;
  77. import java.util.HashSet;
  78. import java.util.List;
  79. import java.util.Set;
  80.  
  81. class ResursionTest {
  82. public static void findSubsets(Set<List<Integer>> allSubsets, List<Integer> nums, int sum) {
  83. if (nums.size() == 0) {
  84. return;
  85. }
  86.  
  87. int currentSum = 0;
  88. for (Integer num : nums) {
  89. currentSum += num;
  90. }
  91.  
  92. // does the current list add up to the needed sum?
  93. if (currentSum == sum) {
  94. allSubsets.add(new ArrayList<>(nums));
  95. }
  96.  
  97. for (int i = 0; i < nums.size(); i++) {
  98. final List<Integer> subset = new ArrayList<>(nums);
  99. subset.remove(i);
  100. findSubsets(allSubsets, subset, sum);
  101. }
  102. }
  103.  
  104.  
  105. public static void main(String[] args) {
  106. int sum = 5;
  107. final Integer[] array = {4, 1, 3, 2};
  108. final HashSet<List<Integer>> allSubsets = new HashSet<>();
  109.  
  110. findSubsets(allSubsets, Arrays.asList(array), sum);
  111. System.out.println(allSubsets);
  112. }
  113. }
  114.  
  115. [[3, 2], [4, 1]]
  116.  
  117. public static void main(String[] args) {
  118. Scanner sc = new Scanner(System.in);
  119. int t = sc.nextInt();
  120. while(t-->0){
  121. int n = sc.nextInt();
  122. int arr[] = new int[n];
  123. for(int i = 0;i<n;i++){
  124. arr[i] = sc.nextInt();
  125. }
  126. int target = sc.nextInt();
  127. int dp[] = new int[target+1];
  128. dp[0] = 1;
  129. int currSum = 0;
  130. for(int i = 0;i<n;i++){
  131. currSum += arr[i];
  132. for(int j = Math.min(currSum,target);j>= arr[i];j--){
  133. dp[j] += dp[j-arr[i]];
  134. }
  135. }
  136. System.out.println(dp[target]);
  137. }
  138. }
Add Comment
Please, Sign In to add comment