This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| recursion [2016/06/07 12:49] – ledyx | recursion [2021/02/07 03:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | = Recursion = | + | ~~REDIRECT> |
| - | http:// | + | |
| - | {{tag> | + | |
| - | + | ||
| - | = 합 = | + | |
| - | <sxh java> | + | |
| - | public static int sum(int n) { | + | |
| - | if(n <= 0) | + | |
| - | return n; | + | |
| - | + | ||
| - | return n + sum(n - 1); | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | = Factorial = | + | |
| - | <sxh java> | + | |
| - | private static int factorial(int n) { | + | |
| - | if(n == 0) //0! = 1 | + | |
| - | return 1; | + | |
| - | else | + | |
| - | return n * factorial(n-1); | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | = Fibonacci Array = | + | |
| - | <sxh java> | + | |
| - | public static int fibonacci(int n) { | + | |
| - | if(n < 1) | + | |
| - | throw new StackOverflowError(" | + | |
| - | + | ||
| - | if(n == 1) { | + | |
| - | return 0; | + | |
| - | } | + | |
| - | else if (n == 2) { | + | |
| - | return 1; | + | |
| - | } | + | |
| - | else { | + | |
| - | return fibonacci(n - 1) + fibonacci(n - 2); | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | = Hanoi Tower = | + | |
| - | <sxh java> | + | |
| - | public static void hanoiTower(int n, String from, String to, String temp) { | + | |
| - | if(n == 1) { | + | |
| - | System.out.println(String.format(" | + | |
| - | } | + | |
| - | else { | + | |
| - | hanoiTower(n - 1, from, temp, to); | + | |
| - | System.out.println(String.format(" | + | |
| - | + | ||
| - | hanoiTower(n - 1, temp, from, to); | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |