1 / 4

# CSC 345 Sample Test 2 - Answers - PowerPoint PPT Presentation

CSC 345 Sample Test 2 - Answers.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CSC 345 Sample Test 2 - Answers ' - hestia

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

publicstaticvoid 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"));

}publicstaticint hashnum(String str){int hnum = 0;for(int i=0;i<str.length();i++) hnum += (int)str.charAt(i); hnum = hnum % 10;return hnum;

}

APPLE = 0PEAR = 6BANANA = 7ORANGE = 4GRAPE = 7

A B C D E F

A 0 1 3 - 9 -

B 1 0 1 4 - 1

C 3 1 0 5 5 2

D - 4 5 0 - -

E 9 - 5 - 0 1

F - 1 2 - 1 0

C -> 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

K

j loop

0

1

2

3

0

1

2

3

4

5

1

publicstaticint comb2(int n, int k) {int[][] c = newint[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]; } }}

1

1

1

2

1

N

i loop

1

3

3

1

1

4

6

4

10

10

1

5

publicstaticint comb(int n, int k) {if(k==0 || k==n)return 1;elsereturn comb(n-1,k-1) + comb(n-1,k);}

Q

Q

Q

Q

Q

Q

Q

Every other pattern is obtainable from one of these by flipping or rotation.

j

i

i

i+1

j+1

i+1

reverse

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.

N! = N factorial = (N)(N-1)(N-2)...(2)(1)