Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

다이나믹 프로그래밍 imporved recursive fibonacci #198

Open
changDDAO opened this issue Jan 9, 2023 · 0 comments
Open

다이나믹 프로그래밍 imporved recursive fibonacci #198

changDDAO opened this issue Jan 9, 2023 · 0 comments

Comments

@changDDAO
Copy link

육안으로 저장된 값들을 확인하면서 동작원리를 생각해보면 이해에 도움이 될 것 같아 간단하게 만들어 봤습니다. 책 너무 잘보고 있습니다.
`package DynamicProgramming;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Fibonacci_ImprovedRecursive {
public static long[] savedValue = new long[100];

public static long fibo(int x){
    if(x==1||x==2){
        return 1;
    }
    if(savedValue[x]!=0){
        return savedValue[x];
    }else {
        savedValue[x]=fibo(x-1)+fibo(x-2);
        return fibo(x-1)+fibo(x-2);
    }

}

public static void main(String[] args)  {
    List<Long> saveLongValue;
    try {
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("피보나치 항을 입력하시오>>");
            int s = sc.nextInt();
            fibo(s);
            System.out.println("배열에 저장된 값>>");
            saveLongValue = Arrays.stream(savedValue).filter(i -> i != 0).boxed().collect(Collectors.toList());
            System.out.println(saveLongValue);
        }
    }catch (Exception e){
        e.printStackTrace();
    }

}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant