【问题标题】:printing fibonacci number series without using loops不使用循环打印斐波那契数列
【发布时间】:2026-02-23 07:10:01
【问题描述】:

有没有一种技巧可以在不调用循环的情况下打印前 n 个斐波那契数

for(int i=1; i<n; i++)
    System.out.println(computeF(n));

来自主程序?

public static int computeF(int n)
{
    if(n==0)
    {
        return 0;
    }
    else if(n==1)
    {
        return 1;
    }
    else
    {
        return computeF(n-1)+computeF(n-2); 
    }

}

可能有一种方法可以在递归中打印中间值,这将打印斐波那契数。

【问题讨论】:

  • 是的,可以做到。任何递归算法都可以序列化。这听起来像是家庭作业。
  • 不,这不是功课!!!我只是在练习

标签: java algorithm fibonacci


【解决方案1】:

你可以使用tail recursion

【讨论】:

  • 尾递归要求函数的最后一条语句调用自身。但在这里我们有:return computeF(n-1)+computeF(n-2)
  • @mike,你从错误的角度看它。斐波那契数列仍然是一个迭代数列,不需要调用自己两次。当您打电话给自己时,您只需要包含更多信息。 ComputeF(d, n1, n2) =&gt; d == 0 ? n1 : ComputeF(d - 1, n2, n2 + n1)。然后以ComputeF(n, 0, 1) 开头
  • 递归其实是一种循环形式
【解决方案2】:
 public class Fid   
  {   
    static int n1=0;
    static int n2=1;
    static int nex=0;
    public static  void fb(int n)
 {
   if(n<10)
    {
      if(n==0)
       {
         System.out.print(" "+n);
         n++;
         fb(n);
       }
        else
           if(n==1)
       {
         System.out.print(" "+n);
         n++;
         fb(n);
       }

        else{ 
            nex=n1+n2;
            System.out.print(" "+nex);
            n1=n2;
            n2=nex;
            n++;
            fb(n);                
            }           
      }        
    }
    public static void main(String[] args)
    {
      fb(0);                                          
    }
}

【讨论】:

    【解决方案3】:

    使用递归:-

      class FibonacciRecursion
    {
    private static int index = 0;
    private static int stoppingPoint = 9;
    
    public static void main (String[] args)
    {
      int n1 = 0;
      int n2 = 1;
    
      fibonacciSequence(n1, n2);
    }
    
    public static void fibonacciSequence(int n1, int n2)
    {
      System.out.println("index: " + index + " -> " + n1);
    
      // make sure we have set an ending point so this Java recursion
      // doesn't go on forever.
      if (index == stoppingPoint)
        return;
    
      // make sure we increment our index so we make progress
      // toward the end.
      index++;
    
      fibonacciSequence(n2, n1+n2);
    }
    }
    

    【讨论】:

      【解决方案4】:

      //Java程序打印斐波那契数列最多n个用户给出的项,而不使用循环

      导入 java.util.* ;

      公共类斐波那契 {

      public static void main(String[] arguments) {

      Scanner s = new Scanner(System.in);
      System.out.print("Enter the no of terms :");
      int no_of_terms= s.nextInt(),a=1,b=0,c=0,count=1;
      System.out.print("0 ");//printing the first term
      fib(no_of_terms,a,b,c,count);}
      

      public static void fib(int no_of_terms,int a,int b,int c,int count) {

      //when value of count will be equal to the no of terms given by user the program will terminate
      
      if (count==no_of_terms)
         System.exit(0);
      else
      {
        count++;
        System.out.print(a+" ");
        c=b;
        b=a;
        a=b+c;//calculating the next term
        fib(no_of_terms,a,b,c,count);//calling the function again with updated value
      }
      

      } }

      【讨论】:

      • 你有什么问题? How to ask
      • 编辑答案中的降价以正确构建代码示例。
      【解决方案5】:
      import java.util.*; 
      public class Fibonacci{
        public static void main(String[] args) {    
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter a no.");
          int n= sc.nextInt(),a=1,b=0,c=0;
          num(n,a,b,c);
        }
        public static void num(int n,int a,int b,int c){
          if(a<=n){
            System.out.println(a);
            c=b;
            b=a;
            a=b+c;
            num(n,a,b,c);
          }
        }
      }
      

      【讨论】:

      • 你去吧,这是可能的最短和最简单的程序
      • 为您的答案添加一些描述。
      • 当我输入93时,我得到了11个数字:Is there a [way] to print the first这个答案如何n Fibonacci numbers without calling a loop?
      最近更新 更多