题目: 2/1, 3/2, 5/3, 8/5, 13/8, 21 13 ...

Posted superdrew

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题目: 2/1, 3/2, 5/3, 8/5, 13/8, 21 13 ...相关的知识,希望对你有一定的参考价值。

java源码:

package studying;

import java.util.Scanner;

public class Sum_Of_FirstN {
    
    /*
     * Topic: There is a score sequence: 2/1, 3/2, 5/3, 8/5, 13/8, 21 13 ... 
     * find the sum of the first 20 of this series.
     */
    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter n :");
        int n = input.nextInt();
        
        double a = 2;
        double b = 1;
        double sum = 0;
        double t;
        
        for(int i = 1; i <= n; i++) {
            sum += a/b;  //this is key point.
            t = a;
            a = a + b;
            b = t;
        }
        System.out.println("The sum of the first n of the sequence:\\n" + sum);
    }

}

结果展示:

 

以上是关于题目: 2/1, 3/2, 5/3, 8/5, 13/8, 21 13 ...的主要内容,如果未能解决你的问题,请参考以下文章