-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] + " "); | ||
} | ||
} | ||
} |