Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Fibonacci Number

Write a program which prints $n$-th fibonacci number for a given integer $n$. The $n$-th fibonacci number is defined by the following recursive formula:

\begin{equation*}
fib(n)= \left \{
\begin{array}{ll}
1　 & (n = 0) \\
1　 & (n = 1) \\
fib(n - 1) + fib(n - 2) & \\
\end{array}
\right.
\end{equation*}

Input

An integer $n$ is given.

output

Print the $n$-th fibonacci number in a line.

Constraints

$0 \leq n \leq 44$

Sample Input 1

3

Sample Output 1

3

Solution in JAVA (judge verdict: Accepted)


import java.util.*;

/**
 * A
 */
public class Main {

    void run() {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        sc.close();

        System.out.println(fib(n));

    }

    int fib(int n) {

        /*
        if(n==0 || n==1) {
            return 1;
        }
        */

        int a = 1;
        int b = 1;
        int tmp = 1;;

        for(int i=1; i<n; i++) {

            tmp = a+b;
            a = b;
            b = tmp;

        }

        return tmp;

    }

    public static void main(String[] args) {
        new Main().run();
    }
}