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

来源:互联网 发布:淘宝男装t恤花色 编辑:程序博客网 时间:2024/06/07 04:57

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

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

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

 

Author

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


0 0
原创粉丝点击