zoukankan      html  css  js  c++  java
  • 斐波拉切

    第一种方法:

    public class Fibonacci2{

     //定义数组方法
     public static void main(String[] args) {
      int arr[] = new int[20];
      arr[0] = arr[1] = 1;
      for (int i = 2; i < arr.length; i++) {
       arr[i] = arr[i - 1] + arr[i - 2]; }
      System.out.println("斐波那契数列的前20项如下所示:");
      for (int i = 0; i < arr.length; i++) {
       if (i % 5 == 0)

       System.out.println();   
       System.out.print(arr[i]+" "); }}}

    第二种方法:

    public class Fibonacci1{

     //定义三个变量方法
     public static void main(String[] args) {
      int a=1, b=1, c=0;
      System.out.println("斐波那契数列前20项为:");
      System.out.print(a + " " + b + " ");
      for (int i = 1; i <= 18; i++) {
       c = a + b;
       a = b;
       b = c;
       System.out.print(c + " ");
       if ((i + 2) % 5 == 0) 
        System.out.println();  }}}

    第三种方法:

    public class Fibonacci3 {

     //使用递归方法
     private static int getFibo(int i) {
      if (i == 1 || i == 2)
      return 1;
      else
      return getFibo(i - 1) + getFibo(i - 2);}

     public static void main(String[] args) {
      System.out.println("斐波那契数列的前20项为:");
      for (int j = 1; j <= 20; j++) {
       System.out.print(getFibo(j) + " ");
       if (j % 5 == 0) 
        System.out.println();}}}

  • 相关阅读:
    1836Alignment
    JS日期格式化
    excle自编公式方法
    excle的公式说明
    小技巧之一 string[]合并
    Nunit的使用小问题
    Ajax中上传文件的方式
    VSS也有BUG?
    SQL Server中将时间型的转为yyyyMMddhhmmss
    给已经存在的PDF文件加水印
  • 原文地址:https://www.cnblogs.com/wanglianjin/p/4888572.html
Copyright © 2011-2022 走看看