(C ++ / JAVA) Fibonacci number

(C ++ / JAVA) has a pair of rabbits from the first 3 months after birth are born every month one pair of rabbits, bunnies grow up to the third month after the month gave birth to one pair of rabbits, if the rabbit is not dead, ask how much each month as the number of rabbits?

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: **** (slightly changed: two output lines Fibonacci number)

/*
 * 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++;
        }
    }
}

Released five original articles · won praise 4 · Views 571

Guess you like

Origin blog.csdn.net/RY2017_Gaoxusheng/article/details/104187889