PS/inflearn java coding2022. 4. 8. 10:39


import java.util.Scanner;


public class Main {
	
	static int []ch;
	static int []input;
	static int n;
	static long []memoization;
	public static void main(String[] args) {
	

		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		Main T = new Main();
		memoization = new long[n];
		
		long start = System.currentTimeMillis();
		for (int i = 0 ; i < n ; i++) {
			System.out.print( T.DFS_M(i) + " ");
		}
		
		
		long end = System.currentTimeMillis();
		System.out.println();
	
		System.out.println("run time : "+ (end-start));
		
		start = System.currentTimeMillis();
		for (int i = 0 ; i < n ; i++) {
			System.out.print( T.DFS(i) + " ");
		}
		end = System.currentTimeMillis();
		System.out.println();
		System.out.println("run time : "+ (end-start));*/
		
	}
	
	public long DFS_M( int L ) {
		
		if ( L < 2 ) {
			return 1;
		} else {
			if( memoization[L-1] != 0 && memoization[L-2] != 0 ) {
				//System.out.println("M[L]:"+L);
				return  memoization[L] = memoization[L-1] + memoization[L-2];
			}
			return memoization[L] = DFS(L - 1) + DFS(L - 2);
		}
	}
	public long DFS( int L ) {
		
		if ( L < 2 ) {
			return 1;
		} else {			
			return DFS(L - 1) + DFS(L - 2);
		}
		
		
	}
	
	
}

'PS > inflearn java coding' 카테고리의 다른 글

초간단 스택  (0) 2022.04.10
멘토링  (0) 2022.04.09
임시반장  (0) 2022.04.09
봉우리  (0) 2022.04.09
소수 구하기, 에라토스테네스 체  (0) 2022.04.09
Posted by easy16