Discussion 9 CS 1101 X Group 5 Lab
Discussion 9 CS 1101 X Group 5
Lab 7 Minor note that default value of int[][] are all 0 default value of boolean[][] are all false
Discussion Plan General notes on Recursion practice PE Problem solving
Recursion Base case Recursive 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); } }
Recursion General approaches Think 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 2 nd last case etc
Recursion Practice Can you write a “while” method that does what the while loop is doing
Recursion Practice Can 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 Practice while(i > 0){ System. out. println(i); i--; } public static void While (int i){ if (i > 0 ){ System. out. println(i); i--; While(i); } }
Recursion Practice Download Practical Test http: //www. comp. nus. edu. sg/~cs 1101 x/3_ca/labs. html Can you write a recursive solution for both questions?
PE Problem Solving 2006/07 Houses in a town Binary image and pattern detection
Houses in a town Need a current_max. Distance and current_max. Point Method to find the distance from (0, 0) to (x, y) Using the final current_max. Distance as radius, find area
Binary image and pattern detection Basically the problem seems very difficult but the general skeleton has been given to you Stuff you might want to try out: rotate 90 CW()
PE Problem Solving 2004/05 Plurals Candles Teams
Plurals Check out String api public boolean ends. With(String suffix) Just a series of if-else and ends. With and substring
Teams Method 1: ◦ Array. List 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]
Other past year questions… Only got the final sourcecode, lost the questions
Snake
Snake “Board” class ◦ void move(char direction) ◦ boolean is. Blank Main ◦ create a “menu”
Snake “Board” class ◦ void move(char direction) ◦ boolean is. Blank Main ◦ create a “menu”
Crossword Puzzle
Boggle
Kings • Can’t really remember what it does • But basically chess game
Do you know… to. String get. Class Array – 1 D, 2 D Array. List – contains, index. Of, get, set, add, remove, remove. First, remove. Last, … String – index. Of, last. Index. Of, starts. With, ends. With, substring, char. At, split Math – min, max, floor, ceil Classes – declare and use
Do you know… Recursion – good to know in case it can be easily solve with Point class Scanner – how to get tokens new Scanner(“some_string”) Regular Expression
- Slides: 24