title Content : A staircase has n-level (n >=0), each walk 1 or 2 levels, from the bottom to the top of the total number of ways to go?
Input Requirements : Only one line of input, and only a number n(if n > 20, then n = n%21, that is, to ensure that the range of N control is: 0 <= n <= 20, when the modulo value of 0 o'clock, Output 1), representing the level of the staircase.
Output Requirements : Only one row, the output from the bottom to the top of the way, followed by a newline.
Reference Sample:
Input: 3
Output: 3
Hint:
Problem decomposition.
Analysis:This topic should be solved with the idea of recursion to deduce the formula. If there is only one staircase, then there is only one way, if there are two steps, then there are two ways to go. If you have N (n > 2) Stairs, because you can go to level one or level two at a time, then we can consider the N-1 and N-2 stairs to the N-level approach, then f (n) = f (N-1) + f (N-2). My Code:
#include <stdio.h>intMain () {intF0, F1, A; intb, N, I; scanf ("%d", &N); N= n% +; 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;}
Standard Answer:
//From Younglee//solve the problem in the different-to, with recursion and no recursion.#include <stdio.h>#include<string.h>#defineN 100#defineRecur 1#defineMODE 0intDealwithrecursion (intf);intDealwithnorecursion (intf);//To Save the result.intArr[n];intMainvoid) { intFloor ; scanf ("%d", &Floor ); Floor%= +; if(MODE = =recur) {printf ("%d\n", Dealwithrecursion (floor)); } Else{memset (arr,0,sizeof(arr)); printf ("%d\n", Dealwithnorecursion (floor)); } return 0;} intDealwithrecursion (intf) {if(f = =1|| f = =0)return 1; return(Dealwithrecursion (F-1) + dealwithrecursion (F-2));} intDealwithnorecursion (intf) {if(Arr[f]! =0)returnArr[f]; intresult; if(f = =0|| f = =1) result =1; Elseresult = Dealwithnorecursion (F-1) + dealwithnorecursion (F-2); ARR[F]=result; returnresult;}
Two methods are used here, recursive and non-recursive.
Somethings about Floors