luishenriique

Problema da Mochila

Jun 26th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. public class Mochila {
  2.  
  3.     public static void main(String[] args) {
  4.         int[] arr = new int[]{11, 8, 7, 6, 5};
  5.         find(arr,20);
  6.     }
  7.  
  8.     public static boolean find( int[] arr,int total){
  9.         return find(arr,0,total);
  10.     }
  11.  
  12.     private static boolean find( int[] arr,int start,  int total){
  13.         if (start == arr.length){
  14.             return false;
  15.         }
  16.         int curr = arr[start];
  17.         if (curr == total){
  18.             System.out.println(curr);
  19.             return true;
  20.         }else if (curr > total || !find(arr,start+1,total-curr)){
  21.             return find(arr,start+1,total);
  22.         }
  23.         System.out.println(curr);
  24.         return true;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment