有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13....求出這個數列的前20項之和。


 

代碼:

 1 public class Main {  2     public static void main (String [] args) {  3         double sum=2;  4         double p=2,q=1,temp;  5         for(int i=1;i<20;i++) {  6             temp=p;  7             p=p+q;  8             q=temp;  9             sum+=p/q; 10  } 11         System.out.println(String.format("%.2f", sum)); 12  } 13 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM