www.rtmj.net > 用JAVA数组来求斐波那契数列前20项: 1 1 2 3 5 8 ….

用JAVA数组来求斐波那契数列前20项: 1 1 2 3 5 8 ….

a1=1; a2=1; s=2; for(int i=2;ia1=a2; a2=s; s=a1+a2; system.out.println(s);

#include<stdio.h> void main() { long f,f1,f2;int i; f1=1;f2=1; printf("%10d%10d",f1,f2);//每个数之间有点距离 for(i=3;i<=20;i++) //i从3开始.因为我希望循环两次之后换行.循环18次.注意这里是小于等于20.{f=f1+f2; printf("%10d",f); f1=f2;f2=

#include<stdio.h> void fac(int s[]) { int i=0,j=1,t; for(t=2;t<20;t++) { s[t]=s[i++]+s[j++]; } } int main() { int s[20]={1,1},i,sum=0; fac(s); for(i=0;i<20;i++) sum+=s[i]; printf("sum=%d\n",sum); return 0; }

方法如下供参考:public class Test { public static void main(String[] args) { int intArrary[] = new int[20]; intArrary[0] = intArrary[1] = 1; for (int i = 2; i < intArrary.length; i++) { intArrary[i] = intArrary[i - 1] + intArrary[i - 2]; } System.out.println("Fibonacci数

用递归是最好的long Factorial(int n) { if (n &lt;= 0) { return 1; } return n * Factorial(n - 1); }

package algorithm; public class Fibonacci { public static void main(final String[] args) { Fibonacci fibonacci = new Fibonacci(); for (int i = 1; i <= 20; i++) { int fibonacciNumber = fibonacci.getFibonacciNumber(i); System.out.println(String.format("

private sub command1_click() dim a(1 to 20) as integer cls a(1) = 1: a(2) = 1 for i = 3 to 20 a(i) = a(i - 2) + a(i - 1) next i for i = 1 to 20 print tab(6 * ((i - 1) mod 5)); a(i); if i mod 5 = 0 then print next i end sub

#includeusing namespace std;int f(int n){ if (n == 1 || n == 2) { return 1; } return f(n -1) + f(n-2);}int main(){ for (int i = 1; i 评论0 0 0

//方法很多,这是一种 int a=0; int b=1; System.out.print("斐波那契数列:1,"); for(int i=0;i<10;i++){ //斐波那契数列加到后面数字会非常大会超出int型的范围,想执行自己替换吧 int c=a+b; System.out.print(c+","); a=b; b=c; }

C语言源程序如下: #include<stdio.h> int main() { int array[100]={1,1};//斐波那契数列前两个元素均为0 int i=0;//循环变量 int n=20;//数列需要求的个数 int sum = 0;//和变量 for(i=2;i<n+1;i++)//按递推原理依次求出后续元素 { array[i]=array[i-1]+

相关搜索:

网站地图

All rights reserved Powered by www.rtmj.net

copyright ©right 2010-2021。
www.rtmj.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com