首页 > 其他 > 详细

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

时间:2017-12-19 23:24:31      阅读:346      评论:0      收藏:0      [点我收藏+]

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 ...

原文:http://www.cnblogs.com/superdrew/p/8067587.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!