Algorithm


 problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1275 

Suppose we put two panes of glass back-to-back. How many ways an are there for light rays to pass through or be reflected after changing direction n times? Following figure shows the situations when the value of n is 0, 1 and 2. Input It is a set of lines with an integer n where 0 ≤ n ≤ 1000 in each of them. Output For every one of these integers a line containing an as described above.

Sample Input 0 1 2

Sample Output 1 2 3

Code Examples

#1 Code Example with C Programming

Code - C Programming

import java.util.*;
import java.math.BigInteger;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        BigInteger seq[] = new BigInteger[1001];
        seq[0] = BigInteger.valueOf(1);
        seq[1] = BigInteger.valueOf(2);
        for(int i=2;i < 1001;i++)
            seq[i] = seq[i-1].add(seq[i-2]);
        
        while(sc.hasNext()){
            int n = sc.nextInt();
            System.out.println(seq[n]);
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
0
1
2

Output

x
+
cmd
1
2
3
Advertisements

Demonstration


UVA Online Judge solution - 10334-Ray Through Glasses - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 10325-The Lottery - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10337 - Flight Planner - UVA Online Judge solution in C,C++,java