Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package Assignment2;
  2.  
  3. public class Tasks {
  4.  
  5.     /* DATASTRUCTURES AND ALGORITHMS 2015-2016
  6.      * Programming assignment 2
  7.      * Deadline: Thursday October 8, 2015, before or at 23:59
  8.      *
  9.      * Name(s) : Tim Nederveen, Ben den Drijver
  10.      * VUid(s) : tnn740, bdr350
  11.      */
  12.  
  13.     // --------------------------------------------------------
  14.     //   METHODS FOR TASK 1 BELOW THIS LINE                     MOET NOG WORDEN GETEST
  15.     // --------------------------------------------------------
  16.  
  17.     public static int task1 (int[] a) {
  18.         //Adapt the algorithm, use the example in the slides.
  19.         if(a.length <= 1){
  20.             return 0;
  21.         }
  22.        
  23.        
  24.         int highestValue = Integer.MIN_VALUE;
  25.  
  26.         rodCuttingRec(a, a.length, highestValue);
  27.        
  28.         return highestValue; //store these highest values in an array, return this array.
  29.     }
  30.  
  31.     private static int rodCuttingRec(int[] a, int arrayLength, int highestValue){
  32.         for(int i = 1; i <= arrayLength; i++){
  33.             highestValue = Math.max(highestValue, rodCuttingRec(a, arrayLength-1, highestValue));
  34.         }
  35.         return highestValue;
  36.     }
  37.    
  38.     // --------------------------------------------------------
  39.     //   METHODS FOR TASK 2 BELOW THIS LINE
  40.     // --------------------------------------------------------
  41.  
  42.     public static int task2 (int[] a) {
  43.         int[] result = new int[a.length];
  44.         result[0] = 0;
  45.        
  46.         for(int j = 1; j < a.length; j++){
  47.             int highestValue = Integer.MIN_VALUE;
  48.             for(int i = 1; i < j; i++){
  49.                 highestValue = Math.max(highestValue, (a[i] + result[j-i]));
  50.             }
  51.             result[j] = highestValue;
  52.         }
  53.         return result[a.length-1];
  54.     }
  55.  
  56.     // --------------------------------------------------------
  57.     //   METHODS FOR TASK 3 BELOW THIS LINE
  58.     // --------------------------------------------------------
  59.  
  60.     public static int task3 (int[][] m) { // m has size n x n (i.e. it is square)
  61.  
  62.         return 0;
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement