怎么
有一分数序列:2.0/1.0,3.0/2.0,5.0/3.0,8.0/5.0,13.0/8.0,21.0/13.0...求出这个数列的前 20 项之和。(15分)题目内容:有一分数序列:2.0/1.0,3.0/2.0,5.0/3.0,8.0/5.0,13.0/8.0,21.0/13.0...求出这个数列的前 20 项之和。
输出格式:
System.out.print(x+"/"+y+"\t");
System.out.printf("\nthe sum is %5.2f",sum);
输入样例:无
输出样例:
2.0/1.0 3.0/2.0 5.0/3.0 8.0/5.0 13.0/8.0 21.0/13.0 34.0/21.0 ......
the sum is 32.66 public class Main {
public static void main(String[] args) {
double x = 2.0;
double y = 1.0;
double temp;
double sum = 0.0;
for (int i = 0; i < 20; i++) {
System.out.print(x + "/" + y + "\t");
sum += x / y;
temp = x;
x = x + y;
y = temp;
}
System.out.printf("\nthe sum is %5.2f", sum);
}
}
页:
[1]