A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lpf870826 中级黑马   /  2015-1-2 09:34  /  1318 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Scanner;

/*求斐波那契数列第n项,n<30,斐波那契数列前10项为
*  1,1,2,3,5,8,13,21,34,55
*/

public class  Test4 {
    public static void main(String[] args) {
        int n;
        Scanner input = new Scanner(System.in);
                do {
                        System.out.print("输入小于30大于0的整数: ");
                        n = input.nextInt();
                        if (n <= 30 && n >0) {
                                int fn = function(n);
                                System.out.println("斐波那契数列第 " + n +" 项为: " + fn );
                        }       
                        else {
                                System.out.println("输入错误请重新输入。");
                        }
                } while( n<= 0 || n > 30);       
    }

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

10 个回复

倒序浏览
n = input.nextInt();  这句加个转换失败的处理会更好哦
回复 使用道具 举报
我以前写的。
  1. /**
  2. * @author 史云龙
  3. * @Descripe
  4. *        斐波纳契数列
  5. */
  6. public class Fibonacci {
  7.         /*
  8.        
  9.         斐波纳契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、
  10.         在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)。
  11.          
  12.          */
  13.         public static void main(String[] args) {       
  14.                 //使用递归的方式打印
  15.                 System.out.print("递归斐波纳契数列:");
  16.                 for(int i=1;i<11;i++){
  17.                         int n1 = fibonacciDiGui(i);
  18.                         System.out.print(n1+",");
  19.                 }
  20.                 System.out.println();
  21.                 //使用递推的方式打印
  22.                 System.out.print("递推斐波纳契数列:");
  23.                 for(int i=1;i<11;i++){
  24.                         int n2 = fibonacciDiTui(i);
  25.                         System.out.print(n2+",");
  26.                 }
  27.                 System.out.println();
  28.                 //使用第三种方式的方式打印
  29.                 System.out.print("第三种方式打印斐波纳契数列:");
  30.                 for(int i=1;i<11;i++){
  31.                         int n2 = fibonacciDiSan(i);
  32.                         System.out.print(n2+",");
  33.                 }
  34.                
  35.         }
  36.        
  37.     /**
  38.      * @param n
  39.      * @return
  40.      * 使用递归方式返回斐波那契数列
  41.      */
  42.     public static int fibonacciDiGui(int n){
  43.         if(n <= 2){  
  44.             return 1;  
  45.         }else{  
  46.             return fibonacciDiGui(n-1) + fibonacciDiGui(n-2);  
  47.         }  
  48.     }
  49.    
  50.     /**
  51.      * @param n
  52.      * @return
  53.      * 使用递推的方式返回斐波那契数列
  54.      */
  55.     public static int fibonacciDiTui(int n){
  56.              if(n <= 2){  
  57.              return 1;  
  58.          }  
  59.          int n1 = 1, n2 = 1, sn = 0;  
  60.          for(int i = 0; i < n - 2; i ++){  
  61.              sn = n1 + n2;  
  62.              n1 = n2;  
  63.              n2 = sn;  
  64.          }  
  65.          return sn;  
  66.     }  
  67.    
  68.     /**
  69.      * @param n
  70.      * @return
  71.      * 递推的一种变种
  72.      */
  73.     public static int fibonacciDiSan(int n){
  74.             int a =0;
  75.             int b=1;
  76.             int c=a+b;
  77.             int x=1;
  78.             if(n<=2){
  79.                     return 1;
  80.             }else{
  81.                     while(x<n-1){
  82.                             if(x%2==0){
  83.                                     b=b+c;
  84.                             }else{
  85.                                     c=b+c;
  86.                             }
  87.                             x++;
  88.                     }
  89.                     if(n%2==0){
  90.                             return b;
  91.                     }else{
  92.                             return c;
  93.                     }
  94.                    
  95.             }
  96.     }
  97. }
复制代码
回复 使用道具 举报
学习了:)
回复 使用道具 举报
1 1 2 3 5 8 11
回复 使用道具 举报
我想用数组弄,我基础题第一题就是这个。。。
回复 使用道具 举报
:handshake,,,,,,,,,,,,
回复 使用道具 举报
其实,我觉得斐波那契数列不需要使用递归,方式,最好采用迭代写法,会高效率一些。例如:
  1. import java.util.*;

  2. public class TestDemo {
  3.     public static void main(String[] agrs) {
  4.         int n= 10;
  5.             for(int i =0 ;i<n;i++) {
  6.                     print(String.format("Fabonacci(%d)=%d",i,fabnacci(i)));
  7.             }
  8.     }

  9.     public static int fabnacci( int n)  {
  10.            
  11.             int a0 = 1,a1=1;
  12.             int current = 0;
  13.             if(n==0 || n ==1) return 1;
  14.             for(int i=2; i<= n;i++)
  15.             {
  16.               current = a0+a1;
  17.               a0 = a1;
  18.               a1 = current ;
  19.             }
  20.             return current;         
  21.     }
  22.   
  23.     public static void  print(Object o) {
  24.             System.out.println(o.toString());
  25.     }

  26. }
复制代码



回复 使用道具 举报
  1. package com.itheima;
  2. import java.util.Scanner;
  3. public class Test2 {

  4.          /**
  5.           * 求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
  6.           * */
  7.    public static void main(String args[]){
  8.           
  9.            //从控制台输入整数代表第n项
  10.            Scanner input=new Scanner(System.in);
  11.            System.out.println("请输入您希望输出斐波那契数列第几项");
  12.            int n=input.nextInt();
  13.           
  14.           //用an来代表斐波那契数列第n项的值
  15.            int an=0;                       
  16.            if(n==1){
  17.                   an=1;
  18.                   System.out.println("斐波那契数列第1项是:"+an);
  19.            }else if(n==2){
  20.                   an=1;
  21.                   System.out.println("斐波那契数列第2项是:"+an);
  22.            }else if(n>=30){
  23.                    System.out.println("请输入小于30的正整数");
  24.            }else {
  25.                    //用递推公式an=an-1+an-2求得斐波那契数列an
  26.                    int i=0;
  27.                    int ax=1, ay=1;
  28.                    for(i=3;i<=n;i++){
  29.                           //用ax代表第an-1项 用ay代表an-2项
  30.                          an=ax+ay;
  31.                         //令ay=an-1,ax=an完成数列的递推
  32.                          ay=ax;
  33.                          ax=an;
  34.                         }
  35.                   System.out.println("斐波那契数列第"+ (i-1) +"项是:"+an);
  36.            }
  37.              
  38.    }
  39.          
  40. }
复制代码
回复 使用道具 举报
顶贴,继续学习去了!
回复 使用道具 举报
路过看看
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马