(C ++ / JAVA)フィボナッチ数

ウサギが死んでいない場合、月はウサギの一組を出産した後に(C ++ / JAVA)はウサギの毎月1組を生まれて、出生後の最初の3ヶ月からウサギのペアを持っている、バニーは、第三の月に育つ、頼みますどのくらいのウサギの数として毎月?

C ++:

#include<iostream>
using namespace std;
int FBNQ(int m){
	if (m == 1 || m == 2)
		return 1;
	else
		return FBNQ(m - 1) + FBNQ(m - 2);
}
int main(){
	int n;
	int p = 1;
	cin >> n;
	while (p <= n){
		cout << FBNQ(p) << "  ";
		p++;
	}
	cout << endl;
}

JAVA:****(わずかに変更された:2本の出力線フィボナッチ数)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dm;

import java.util.Scanner;

/**
 *
 * @author Lenovo
 */
public class DM {

    /**
     * @param args the command line arguments
     */
    public static int FBNQ(int m) {
        if (m == 1 || m == 2) {
            return 1;
        } else {
            return FBNQ(m - 1) + FBNQ(m - 2);
        }
    }

    public static void main(String[] args) {
        int a, b;
        int p = 1,q=1;
        Scanner s = new Scanner(System.in);
        a = Integer.parseInt(s.next());
        b = s.nextInt();
        while (p <= a) {
            System.out.print(FBNQ(p)+" ");
            p++;
        }
        System.out.println();
        while (q <= b) {
            System.out.print(FBNQ(q)+" ");
            q++;
        }
    }
}

リリース5元の記事 ウォンの賞賛4 ビュー571

おすすめ

転載: blog.csdn.net/RY2017_Gaoxusheng/article/details/104187889