2012-06-02 17:04:21Morris

[UVA][費式] 10334 - Ray Through Glasses


  Ray Through Glasses 

Suppose we put two panes of glass back-to-back. How many ways 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 as described above.

Sample Input 

0
1
2

Sample Output 

1
2
3

 

#include <stdio.h>
#include <stdlib.h>
int f[1001][301] = {};
int main() {
    int i, j;
    f[0][0] = 1, f[1][0] = 2;
    for(i = 2; i <= 1000; i++) {
        for(j = 0; j < 300; j++) {
            f[i][j] = f[i-1][j] + f[i-2][j];
        }
        for(j = 0; j < 300; j++) {
            f[i][j+1] += f[i][j]/10;
            f[i][j] %= 10;
        }
    }
    while(scanf("%d", &i) == 1) {
        j = 299;
        while(f[i][j] == 0) j--;
        while(j >= 0)
            putchar(f[i][j]+'0'), j--;
        puts("");
    }
    return 0;
}