Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Assignment2;
- public class Tasks {
- /* DATASTRUCTURES AND ALGORITHMS 2015-2016
- * Programming assignment 2
- * Deadline: Thursday October 8, 2015, before or at 23:59
- *
- * Name(s) : Tim Nederveen, Ben den Drijver
- * VUid(s) : tnn740, bdr350
- */
- // --------------------------------------------------------
- // METHODS FOR TASK 1 BELOW THIS LINE MOET NOG WORDEN GETEST
- // --------------------------------------------------------
- public static int task1 (int[] a) {
- //Adapt the algorithm, use the example in the slides.
- if(a.length <= 1){
- return 0;
- }
- int highestValue = Integer.MIN_VALUE;
- rodCuttingRec(a, a.length, highestValue);
- return highestValue; //store these highest values in an array, return this array.
- }
- private static int rodCuttingRec(int[] a, int arrayLength, int highestValue){
- for(int i = 1; i <= arrayLength; i++){
- highestValue = Math.max(highestValue, rodCuttingRec(a, arrayLength-1, highestValue));
- }
- return highestValue;
- }
- // --------------------------------------------------------
- // METHODS FOR TASK 2 BELOW THIS LINE
- // --------------------------------------------------------
- public static int task2 (int[] a) {
- int[] result = new int[a.length];
- result[0] = 0;
- for(int j = 1; j < a.length; j++){
- int highestValue = Integer.MIN_VALUE;
- for(int i = 1; i < j; i++){
- highestValue = Math.max(highestValue, (a[i] + result[j-i]));
- }
- result[j] = highestValue;
- }
- return result[a.length-1];
- }
- // --------------------------------------------------------
- // METHODS FOR TASK 3 BELOW THIS LINE
- // --------------------------------------------------------
- public static int task3 (int[][] m) { // m has size n x n (i.e. it is square)
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement