Somethings about Floors,

Source: Internet
Author: User

Somethings about Floors,

Question content: There are N levels (N> = 0) in a stair. How many steps are there from the bottom to the top of every step of level 1 or Level 2?

Input requirements: Only one row of input, and only one number N(If N> 20, N = N % 21 ensures that the range of N is: 0 <= N <= 20. When the value after the modulo operation is 0, output 1), Representing the level of the stair.

Output requirements: There is only one line. The output goes from the bottom to the top, and there is a line break behind it.

Example:

Input: 3

Output: 3

 

Hint:

Problem breakdown.

Analysis:This question should be solved by recursive thinking. If there is only one level of stairs, there is only one way to walk; if there are two levels of stairs, there are two ways to walk ;... if there are N (N> 2) level stairs, because one can go up level one or two, then we can consider steps from N-1 level and N-2 level stairs to level N, then F (N) = F (N-1) + F (N-2 ). My code:
#include <stdio.h>int main() {    int f0, f1, a;    int b, n, i;    scanf("%d", &n);    n = n % 21;    if (n == 0 || n == 1) {        printf("%d\n", 1);        return 0;    }    f0 = 1;    f1 = 2;    for (i = 3; i <= n; i++) {        a = f0 + f1;        f0 = f1;        f1 = a;    }    printf("%d\n", f1);    return 0;}

Answer:

// from younglee// solve the problem in two different way, with recursion and no recursion.#include<stdio.h>#include<string.h>#define N 100 #define RECUR 1#define MODE 0 int dealWithRecursion(int f);int dealWithNoRecursion(int f);// to save the result.int arr[N]; int main(void) {    int floor;    scanf("%d", &floor);    floor %= 21;    if (MODE == RECUR) {        printf("%d\n", dealWithRecursion(floor));    } else {        memset(arr, 0, sizeof(arr));        printf("%d\n", dealWithNoRecursion(floor));    }    return 0;} int dealWithRecursion(int f) {    if (f == 1 || f == 0) return 1;    return (dealWithRecursion(f - 1) + dealWithRecursion(f - 2));} int dealWithNoRecursion(int f) {    if (arr[f] != 0) return arr[f];    int result;    if (f == 0 || f == 1) result = 1;    else result = dealWithNoRecursion(f - 1) + dealWithNoRecursion(f - 2);    arr[f] = result;    return result;}

 

Here we use two methods: recursion and non-recursion.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.