免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Java 斐波切納序列
package txlong.practice;

import java.util.Scanner;
import org.junit.Test;
/**
 *
使用迭代和遞歸求 斐波那契數(shù)列
 * @author txlong2012
 *
 */
public class FeiboqienaSequence {

        @Test
        public void test() {
              //  int result = getByIteration();
                System.out.println(getByRecursion());
        }
        /**
         * 迭代實(shí)現(xiàn)斐波那契數(shù)列
         * @return 輸出最后一個(gè)序列值,別切打印出整個(gè)斐波那契數(shù)列
         */
        public int getByIteration() {
                int result = 0;
                Scanner scanner  = new Scanner(System.in);
                int n = Integer.parseInt(scanner.next().trim());
                while( n < 0) {
                        System.out.println("必須輸入一個(gè)正整數(shù)");
                        n = Integer.parseInt(scanner.next().trim());
                }
                scanner.close();
                
                if(n == 1|| n== 2) {
                        result = 1;
                        if(n == 1){
                                System.out.print("得到斐波切納序列:" + 1 +"\t");
                        }else{
                                System.out.print("得到斐波切納序列:" + 1 +"\t" + 1 + "\t");
                        }
                }else {
                        int f0 = 1;
                        int f1 = 1;
                        int count = 3;
                        System.out.print("得到斐波切納序列:" + f0 +"\t" + f1 + "\t");
                        while( count <= n){
                                result = f0 + f1;
                                System.out.print(result + "\t");
                                f0 = f1;
                                f1 = result;
                                count++;
                        }
                }
                return result;
        }
        /**
         * 從控制臺(tái)獲取斐波那契的輸入?yún)?shù) n
         * @return 數(shù)列最后一個(gè)數(shù)值
         */
        public int getByRecursion() {
                int result = 0;
                Scanner scanner  = new Scanner(System.in);
                int n = Integer.parseInt(scanner.next().trim());
                while( n < 0) {
                        System.out.println("必須輸入一個(gè)正整數(shù)");
                        n = Integer.parseInt(scanner.next().trim());
                }
                scanner.close();
                return recursion(n);
        }
        /**
         * 遞歸實(shí)現(xiàn)斐波那契數(shù)列
         * @param n 斐波那契參數(shù)
         * @return 數(shù)列最后一個(gè)數(shù)值
         */
        public int recursion(int n) {
                if(n == 1 || n == 2) { 
                        return 1;
                }
                return recursion(n-1) + recursion(n - 2);    
        }
}


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
斐波那契數(shù)列,圖示
線程“main”中的異常java.lang.NumberFormatException:對(duì)于輸入字符串:“t1”?
斐波那契
548,動(dòng)態(tài)規(guī)劃解最長(zhǎng)的斐波那契子序列的長(zhǎng)度
java基礎(chǔ)習(xí)題(一)
遞歸與迭代的區(qū)別
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服