Discussion 9 CS1101X Group 5. Lab7 Minor note that default value of int[][] are all 0 default value...
-
Upload
edward-leonard -
Category
Documents
-
view
217 -
download
0
Transcript of Discussion 9 CS1101X Group 5. Lab7 Minor note that default value of int[][] are all 0 default value...
RecursionBase caseRecursive case
n! = n * (n-1) * (n-2) * … * 2 * 1 for n > 0 (recursive case)
0! = 1 (base case)
int fac(int n){
if (n == 0) return 1;
else{ return n * fac(n-1); }
}
RecursionGeneral approachesThink recursive
◦Split the problem into simple cases◦Work in the opposite way
Work from a base case See what you need to do for the 2nd last
case etc
Recursion PracticeCan you write a “while” method
that does what the while loop is doing
public static void While (int i){ if (cond){ //code of while body While(i); }}
Recursion Practicewhile(i > 0){
System.out.println(i);
i--;
}
public static void While (int i){
if (i > 0 ){
System.out.println(i);
i--;
While(i);
}
}
Recursion PracticeDownload Practical Test
http://www.comp.nus.edu.sg/~cs1101x/3_ca/labs.html
Can you write a recursive solution for both questions?
Houses in a townNeed a current_maxDistance and
current_maxPointMethod to find the distance from (0,0) to
(x,y)Using the final current_maxDistance as
radius, find area
Binary image and pattern detectionBasically the problem seems very
difficult but the general skeleton has been given to you
Stuff you might want to try out:rotate90CW()
PluralsCheck out String api
public boolean endsWith(String suffix)
Just a series of if-else and endsWith and substring
TeamsMethod 1:
◦ArrayList and count and remove Method 2:
◦Keep a String[] and each time you allocate a player, set the index to null and go to the next index (index + 1) % n
◦Do until allocate n players◦Only increment the “count” when
you are at non-null String[location]
Do you know…toStringgetClassArray – 1D, 2DArrayList – contains, indexOf, get, set, add,
remove, removeFirst, removeLast,…
String – indexOf, lastIndexOf, startsWith, endsWith, substring, charAt, split
Math – min, max, floor, ceilClasses – declare and use