斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, …,其递推通项公式为: F1=F2=1 (n=l, 2) Fn=Fn-1+Fn-2 (n>=3) 为求出第n项的值,请编写程序。
public static void main(String[] args) { int first=1; int second=1; int xiang=0; for (int i =3; i <=100; i++) {//第三项,前两项固定值 xiang=first+second; first=second; second=xiang; } System.out.println(xiang); } }
求下式的值:
public static void main(String[] args) { double s=0.0;//三个循环各自累加,然后求和 for (int i = 1; i <=100; i++) { s+=i; } for (int j = 1; j <=50 ; j++) { s+=j*j; } for (int k = 1; k <=10; k++) { s+=1.0/k; } System.out.println(s); } }