# CSC 345 Sample Test 2 - Answers APPLE = 0 PEAR = 6 BANANA = 7 ORANGE = 4 GRAPE = 7 public static void prob1() { System.out.println("APPLE = " + hashnum("APPLE"));

## Presentation on theme: "CSC 345 Sample Test 2 - Answers APPLE = 0 PEAR = 6 BANANA = 7 ORANGE = 4 GRAPE = 7 public static void prob1() { System.out.println("APPLE = " + hashnum("APPLE"));"— Presentation transcript:

CSC 345 Sample Test 2 - Answers APPLE = 0 PEAR = 6 BANANA = 7 ORANGE = 4 GRAPE = 7 public static void prob1() { System.out.println("APPLE = " + hashnum("APPLE")); System.out.println("PEAR = " + hashnum("PEAR")); System.out.println("BANANA = " + hashnum("BANANA")); System.out.println("ORANGE = " + hashnum("ORANGE")); System.out.println("GRAPE = " + hashnum("GRAPE")); } public static int hashnum(String str) { int hnum = 0; for(int i=0;i D -> E -> G -> B -> A -> F -> C total dist = 20 There are a number of ways to answer this question. Finding a shorter tour, assuming one exists is the most straightforward way to do this. A -> C -> D -> E -> G -> B -> F -> A total dist = 18

public static int comb2(int n, int k) { int[][] c = new int[n+1][k+1]; int p; if(k>n) return 0; else { for(int i=0;i<=n;i++) { p = Math.min(i,k); for(int j=0;j<=p;j++) { if(j==0 || j==i) c[i][j] = 1; else c[i][j] = c[i-1][j-1] + c[i-1][j]; } } return c[n][k]; } } } 6! = 720 1 11 121 133 1 146 4 15 012345012345 012 3 N i loop K j loop 10 public static int comb(int n, int k) { if(k==0 || k==n) return 1; else return comb(n-1,k-1) + comb(n-1,k); }

Q Q Q Q Q Q Q Q Every other pattern is obtainable from one of these by flipping or rotation. i j i+1 j+1 i i+1 j j+1 Imagine that the line connecting the list S of points in R2 crosses over itself at some location in the list. Lets say that line segment connecting point i to point i+1 is crossed over at some point by the line segment connecting points j and j+1. These four points represent a generic four-sided figure (quadrilateral) in which the line segments defined here are the internal diagonals. If we assume that the sum of the lengths of these two line segments is greater than the sum of the lengths of the opposite sides of the quadrilateral the we can reorder the points in S to achieve a shorter total line length. That is if dist(i,i+1) + dist(j,j+1) is greater than dist(i,j) + dist(i+1,j+1) then we can reverse the order of the list of points between i+1 and j, inclusive to obtain a shorter total line length. reverse N! = N factorial = (N)(N-1)(N-2)...(2)(1)

Download ppt "CSC 345 Sample Test 2 - Answers APPLE = 0 PEAR = 6 BANANA = 7 ORANGE = 4 GRAPE = 7 public static void prob1() { System.out.println("APPLE = " + hashnum("APPLE"));"

Similar presentations