Skip to content

Commit

Permalink
Section7-4. 피보나치 수열(메모리제이션)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuseon25 committed Jan 26, 2025
1 parent 6655bdd commit 083a073
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Section07/피보나치_수열.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package 인프런.Section07;

import java.util.Scanner;

public class 피보나치_수열 {

static int[] arr;

public int recursive(int n) {
if(arr[n] > 0) return arr[n];
else if(n == 1 || n == 2) return arr[n] = 1;
else return arr[n] = recursive(n - 2) + recursive(n - 1);
}


public static void main(String[] args) {
피보나치_수열 t = new 피보나치_수열();
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
arr = new int[n+1];

t.recursive(n);
for(int i = 1; i <= n; i++) {
System.out.print(arr[i] + " ");
}
}
}

0 comments on commit 083a073

Please sign in to comment.