码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-20 03:39:47      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:int   out   print   top   studying   ble   gpo   http   ase   

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

标签:int   out   print   top   studying   ble   gpo   http   ase   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!