題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13…求出這個數列的前20項之和。 – JAVA編程語言程序開發技術文章

[java]
<span style="font-size:24px;BACKGROUND-COLOR: #ff0000"><strong>有什麼好的意見歡迎大傢提醒,謝謝!</strong></span> 
[java]
/*題目:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13…求出這個數列的前20項之和。   
1.程序分析:請抓住分子與分母的變化規律。*/ 
<span style="BACKGROUND-COLOR: #66ff99">public class Series { 
    public static void main(String[]args){ 
        int b = 2; 
        int a = 1; 
        double sum = 0; 
        for(int i = 0;i < 20;i++){ 
            if(i%5 == 0){ 
                System.out.println(); 
                System.out.print(b+"/"+a+"+"); 
                int temp = 0; 
                sum += b/a; 
                temp = a; 
                a = b; 
                b = temp + b; 
            }else if(i == 19){ 
                System.out.print(b+"/"+a+"="); 
                int temp = 0; 
                sum += b/a; 
                temp = a; 
                a = b; 
                b = temp + b; 
            }else{ 
                System.out.print(b+"/"+a+"+"); 
                int temp = 0; 
                sum += b/a; 
                temp = a; 
                a = b; 
                b = temp + b; 
            } 
        } 
        System.out.println(sum); 
    } 

</span> 

摘自 djw10962000的專欄

發佈留言