题目:
有一分数序列: 2/1 , 3/2 , 5/3 , 8/5 , 13/8 , 21/13... 求出这个数列的前 20 项之和。
程序分析:
请抓住分子与分母的变化规律。
1 public class _020ProgressionSum { 2 3 public static void main(String[] args) { 4 progressionSum(); 5 } 6 7 private static void progressionSum() { 8 int numerator = 2; 9 int denominator = 1; 10 int temp = 1; 11 double sum = 0; 12 13 for (int i = 1; i <= 20; i++) { 14 sum = sum + (double) numerator / denominator; 15 temp = denominator; 16 denominator = numerator; 17 numerator = denominator + temp; 18 } 19 20 System.out.println("前20项相加的和是:" +" "+ sum); 21 } 22 23 }