Skip to content

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

Description

@changDDAO

육안으로 저장된 값들을 확인하면서 동작원리를 생각해보면 이해에 도움이 될 것 같아 간단하게 만들어 봤습니다. 책 너무 잘보고 있습니다.
`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();
    }

}

}
`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions