当前位置:网站首页>Fibonacci number of dynamic programming

Fibonacci number of dynamic programming

2022-07-07 23:29:00 Little brother who loves Python

Dynamic programming

Fibonacci number

/*  Fibonacci sequence  */
public class Fit {
    

    public static int fit(int n){
    
        if(n<2){
    
            return n;
        }
        int[] dp = new int[n+1];
        // Initialization of boundary value 
        dp[1] = 1;
        for (int i = 2; i <= n ;i++){
    
            // State transition equation 
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }

    public static void main(String[] args) {
    
        int num = 0;
        Scanner scanner = new Scanner(System.in);

        if (scanner.hasNextInt()){
    
           num = scanner.nextInt();
           fit(num);
           System.out.println(fit(num));
        }else{
    
            System.out.println(" The input is not a natural number !");
        }
    }

}```

原网站

版权声明
本文为[Little brother who loves Python]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130559258553.html