zoukankan      html  css  js  c++  java
  • 99-Flagstone Walk

    The main hall of UESTC is far from the dormitory. However, poor Hongshu has to go there every day to talk with his teachers. This is the most unhappy thing on the earth. However, Hongshu is so optimistic that he still digs a lot of fun during that boring period.

    There is a long flagstone walk on the way from the dormitory to the main hall. This flagstone walk has NN lines and four flagstones arranged in each line. Hongshu always start from rightmost flagstone of the first line. In order to make this walk funny, he would always step onto the left or right flagstone in the next line if there is. For example, if Hongshu stands at the second rightmost flagstone of the third line, he would choose to step to the first or third rightmost one of the forth line. Note that Hongshu has only one choice when he is at the corner of one line.

    Because Hongshu has to go to the main hall every day, he wants to know how many different ways to step across the flagstone walk. Can you help him?

    Standard Input

    The first line of the input is an integer T (T20), which stands for the number of test cases you need to solve.
    Each case consists of an integer N (1N20) on a single line, which stands for the length of the walk.

    Standard Output

    For each case, print the number of ways on a single line.

    Samples

    InputOutput
    3
    2
    3
    4
    1
    2
    3

    Note

    Hongshu has 33 ways to choose when the length of walk is 44.


    很有意思的一个题目。英文描述有问题,但看图应该能明白。最后发现是一个斐波拉契数列

    #include <stdio.h>
    
    int fib(int n);
    
    int main() {
        setbuf(stdout, NULL);
    
        int T, N, result;
    
        scanf("%d", &T);
        while (T--) {
            scanf("%d", &N);
            result = fib(N - 1);
            printf("%d
    ", result);
        }
        return 0;
    }
    
    int fib(int n) {
        int a = 1;  
        int b = 2;  
        int c = 0;  
        int i;
        if (n == 0) return 1;
        if (n == 1) return 1;
        if (n == 2) return 2;
        while (n > 2) {
            for (i = 0; i < n - 2; i++) {
                c = a + b;  
                a = b;
                b = c;
                n--;
            }
        }
        return c;
    }
  • 相关阅读:
    (4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中
    (3)Maven快速入门_3在Eclipse中创建Maven项目打包成jar
    (2)Maven快速入门_2maven在Eclipse中的设置
    (1)Maven快速入门_1maven安装
    (11)Microsoft office Word 2013版本操作入门_word中表格操作
    (10)Microsoft office Word 2013版本操作入门_word表格
    洛谷 P2144 [FJOI2007]轮状病毒
    洛谷 P2234 [HNOI2002]营业额统计
    【模板】主席树
    洛谷 P2572 [SCOI2010]序列操作
  • 原文地址:https://www.cnblogs.com/dch0319/p/14091063.html
Copyright © 2011-2022 走看看