Download presentation
Presentation is loading. Please wait.
Published byEdward Leonard Modified over 9 years ago
1
Discussion 9 CS1101X Group 5
2
Lab7 Minor note that default value of int[][] are all 0 default value of boolean[][] are all false
3
Discussion Plan General notes on Recursion Recursion practice PE Problem solving
4
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); } }
5
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
6
Recursion Practice Can you write a “while” method that does what the while loop is doing
7
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); }
8
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); }
9
Recursion Practice Download Practical Test http://www.comp.nus.edu.sg/~cs1101x/3_ ca/labs.html http://www.comp.nus.edu.sg/~cs1101x/3_ ca/labs.html Can you write a recursive solution for both questions?
10
PE Problem Solving 2006/07 Houses in a town Binary image and pattern detection
11
Houses in a town Need a current_maxDistance and current_maxPoint Method to find the distance from (0,0) to (x,y) Using the final current_maxDistance as radius, find area
12
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: rotate90CW()
13
PE Problem Solving 2004/05 Plurals Candles Teams
14
Plurals Check out String api public boolean endsWith(String suffix)String Just a series of if-else and endsWith and substring
15
Teams Method 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]
16
Other past year questions… Only got the final sourcecode, lost the questions
17
Snake
18
“Board” class ◦ void move(char direction) ◦ boolean isBlank Main ◦ create a “menu”
19
Snake “Board” class ◦ void move(char direction) ◦ boolean isBlank Main ◦ create a “menu”
20
Crossword Puzzle
21
Boggle
22
Kings Can’t really remember what it does But basically chess game
23
Do you know… toString getClass Array – 1D, 2D ArrayList – contains, indexOf, get, set, add, remove, removeFirst, removeLast,… String – indexOf, lastIndexOf, startsWith, endsWith, substring, charAt, split Math – min, max, floor, ceil Classes – declare and use
24
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.