[JAVA] μ¬κ·ν¨μ ( λ©λͺ¨μ΄μ μ΄μ )
μ¬κ· μκ³ λ¦¬μ¦
μ¬κ·(Revursion) ν¨μλ νΉμ ν¨μ λ΄μμ μμ μ λ€μ νΈμΆνμ¬ λ¬Έμ λ₯Ό ν΄κ²°ν΄λκ°λ ν¨μμ΄λ€. λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ μλ λ²μμ λ¬Έμ μμ λ μμ λ²μμ νμ λ¬Έμ λ₯Ό ν΄κ²°ν¨μΌλ‘μ¨ μλ λ¬Έμ λ₯Ό ν΄κ²°ν΄ λκ°λ λ°©μμ΄λ€. μΌλ° λ°λ³΅λ¬Έμ ν΅ν΄ ꡬν κ°λ₯ν κΈ°λ₯μ μ¬κ· ν¨μλ₯Ό ν΅ν΄ ꡬνμ΄ κ°λ₯νλ©° λ°λλ‘ μ¬κ·ν¨μλ‘ κ΅¬ν κ°λ₯ν κΈ°λ₯μ λ°λ³΅λ¬ΈμΌλ‘ ꡬνμ΄ κ°λ₯νλ€.
μ¬κ· ν¨μλ ν¨μ λ΄μμ μκΈ° μμ μ κ³μ νΈμΆνλ λ°©μμ΄κΈ° λλ¬Έμ ν¨μ μμ λ°λμ μ’ λ£ κ΅¬κ°μ΄ λλ BaseCaseλ₯Ό μκ°νμ¬ μ½λλ₯Ό ꡬνν΄μΌ νλ€. μλλ μνμμ μ΄λ€.
public class Recursion
{
public static void main(String[] args)
{
Function();
}
public static void Function()
{
System.out.println("λ°κ°μ΅λλ€");
Function();
}
}
Funtion ν¨μμ κΈ°λ₯μ "λ°κ°μ΅λλ€"λ₯Ό νΈμΆνκ³ λ€μ μκΈ° μμ μ νΈμΆνκ³ μλ€. ν΄λΉ μμ€μμ λ¬Έμ λ μκΈ° μμ μ νΈμΆλ§νκ³ μμ§ ν¨μ μμμ΄ μ’ λ£λλ ꡬκ°μ΄ μκΈ° λλ¬Έμ 무νλ°λ³΅λλ 루νμ λΉ μ§κ² λλ€.
public class Recursion_Test
{
public static void main(String[] args)
{
Function(5);
}
public static void Function()
{
if(num==0)
{
return;
}
else
{
System.out.println("λ°κ°μ΅λλ€.");
Function(num-1);
}
}
μ΄λ κ² μμ ν κ²°κ³Ό, λ§€κ°λ³μ κ°μ΄ 0μΌ κ²½μ° returnμ νκ³ μλκ²½μ° "λ°κ°μ΅λλ€"λ₯Ό μΆλ ₯νκ³ num-1μ ν΄λΉνλ κ°μ λ§€κ°λ³μλ‘ μ λ¬νκ³ μλ€. μ¬κΈ°μ num μ΄ 0μΌ κ²½μ° returnμ νκ² λλ κ΅¬λ¬Έμ΄ μ¬κ· ν¨μμ BaseCaseꡬκ°μ΄ λλ€.
ν΄λΉ μ¬μ¬ ν¨μμ νΈμΆμ κ·Έλ¦ΌμΌλ‘ 보면 μλμ κ°λ€.
μμ : 1λΆν° nκΉμ§ ν© κ΅¬νκΈ° ( recursive )
μμμ λ°°μ΄ μ¬κ· μκ³ λ¦¬μ¦μ μ μ©νμ¬ 1λΆν° nκΉμ§ ν©μ ꡬνλ μ¬κ·ν¨μλ₯Ό ꡬνν΄λ³Έλ€.
public class Recursion
{
public static void main(String[] args)
{
System.out.println("1λΆν° 5κΉμ§μ ν©μ : " + Function(5));
}
public static int Function(int num)
{
if(num == 1)
{
return 1;
}
else
{
return num + Function(num -1);
}
}
}
μμ : μ λ ₯λ°μ μ μ nμ 2μ§μλ‘ μΆλ ₯νκΈ° ( recursive )
class Recursive {
public void recursive(int n) {
if(n==0)
return;
else{
recursive(n/2);
System.out.print(n%2);
}
}
public static void main(String[] args) {
Solution T=new Solution();
T.recursive(11);
}
}
//////////
μ
λ ₯ : 11
μΆλ ₯ : 1011
μμ : ν©ν λ¦¬μΌ ( recursive )
μ λ ₯λ°μ μ μ nμ ν©ν λ¦¬μΌ κ°μ μΆλ ₯νμμ€.
public class Recursive {
// λ°νλ°μμΌνλ int
public int recursive(int n) {
if(n==1) return 1;
else return n*recursive(n-1);
}
public static void main(String[] args) {
Solution T=new Solution();
System.out.println(T.recursive(5));
}
}
//////////
μ
λ ₯ : 5
μΆλ ₯ : 5*4*3*2*1 = 120
μμ : νΌλ³΄λμΉ ( recursive )
1. κ°μ₯ κΈ°λ³Έμ μΈ νΌλ³΄λμΉ recursive
public class Solution {
public int recursive(int n) {
if(n==1) return 1;
else if(n==2) return 1;
else return recursive(n-1)+recursive(n-2);
}
public static void main(String[] args) {
Solution T=new Solution();
int n=10;
for(int i=1; i<=n; i++) System.out.print(T.recursive(i)+" ");
}
}
2. μ‘°κΈ λ μ΅μ νλ νΌλ³΄λμΉ recursive
public class Solution {
static int[] fibo;
public int recursive(int n) {
if(n==1) return fibo[n]=1;
else if(n==2) return fibo[n]=1;
else return fibo[n]=recursive(n-1)+recursive(n-2);
}
public static void main(String[] args) {
Solution T=new Solution();
int n=10;
fibo=new int[n+1];
T.recursive(n);
for(int i=1; i<=n; i++) System.out.print(T.recursive(i)+" ");
}
}
λ©λͺ¨μ΄μ μ΄μ μ νμ©ν νΌλ³΄λμΉ recursive ( κ°μ₯ μ΅μ ν )
λ©λͺ¨μ΄μ μ΄μ (memoization)μ΄λ ?
μ»΄ν¨ν° νλ‘κ·Έλ¨μ΄ λμΌν κ³μ°μ λ°λ³΅ν΄μΌ ν λ, μ΄μ μ κ³μ°ν κ°μ λ©λͺ¨λ¦¬μ μ μ₯ν¨μΌλ‘μ¨ λμΌν κ³μ°μ λ°λ³΅μνμ μ κ±°νμ¬ νλ‘κ·Έλ¨ μ€ν μλλ₯Ό λΉ λ₯΄κ² νλ κΈ°μ . λμ κ³νλ²μ ν΅μ¬μ΄ λλ κΈ°μ μ΄λ€.
λ©λͺ¨μ΄μ μ΄μ μ μ¬μ©νμ¬ λ°°μ΄μ νλ λ§λ€κ³ κ°μ μ¬μ¬μ©νμ¬ νμ΄ν κ²°κ³Ό, μ¬κ·λ§μ μ¬μ©ν μμ νμ΄ λ°©λ²κ³Ό λΉκ΅νμ¬ μ€νμλμ μμ²λ μ°¨μ΄κ° μλ€.
public class Solution {
static int[] fibo;
public int recursive(int n) {
if(fibo[n]>0) return fibo[n];
if(n==1) return fibo[n]=1;
else if(n==2) return fibo[n]=1;
else return fibo[n]=recursive(n-2)+recursive(n-1);
}
public static void main(String[] args) {
Solution T=new Solution();
int n=45;
fibo=new int[n+1];
T.recursive(n);
for(int i=1; i<=n; i++) System.out.print(T.recursive(i)+" ");
}
}