C/C++经典程序训练2---斐波那契数列

来源:互联网 发布:矩阵相似矩阵的迹 编辑:程序博客网 时间:2024/06/05 15:02

Problem Description

编写计算斐波那契(Fibonacci)数列的第n项函数fib(n)(n<40)。
数列:
f1=f2==1;
fn=fn-1+fn-2(n>=3)。

Input

输入整数n的值。

Output

输出fib(n)的值。

Example Input

7

Example Output

13

Hint

import java.util.Scanner;import java.util.*;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(System.in);int n = cin.nextInt();System.out.println(fib(n));}public static int fib(int n) {int b = 0;if (n == 1 || n == 2) {b = 1;} else {            b=fib(n-1)+fib(n-2);}return b;}}


0 0