Download presentation
Presentation is loading. Please wait.
Published byEvangeline Watkins Modified over 8 years ago
2
Recursive Problem 1 Write a java class named my_WordReverse using recursion. It takes a sentence and returns the sentence in reverse order. Note: Space is the only separator.
3
Something About Java Import the corresponding packages –import java.io.* –import java.util.* … If you use some IDE, be sure your program can run just with standard JDK. Check whether you use some other package: import com.borland.~~~
4
Something About Java Input Part BufferedReader keyboard = new BufferedReader(new InputStreamReader (System.in) ); –System ; final class –InputStreamReader; a bridge from byte streams to character streams –BufferedReader; for efficiency
5
Something About Java Get words from sentence –Use space as separator Use String.substring() String.indexOf() String.length() –Use StringTokenizer StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }
6
Something About Java Store Words into …… –Array –ArrayList –Vector
7
Recursive Algorithm Sample online –n recursive calls Swap the the last one and first one –n/2 recursive calls
8
Problem 2 Make Changes For a given amount of money, print out all possible solutions of making changes. Complete and No duplicate!
9
Something About Java Integer.parseInt(String s) Variable’s scope –http://java.sun.com/docs/books/tutorial/java/nut sandbolts/scope.htmlhttp://java.sun.com/docs/books/tutorial/java/nut sandbolts/scope.html
10
Recursive Algorithm 50c 25c 10c 10c?10c 5c
11
Recursive Algorithm Sample online Why complete and no duplicate? Another example Why it is wrong? Wrong use of “If – else” causes lost cases. For example, 20 cents No (1,0,10) and (1,1,5) Larger amount More solutions lost!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.