Current location - Recipe Complete Network - Complete cookbook - Ladder menu
Ladder menu
Suppose that there are f(n) methods in the n-lattice ladder.

Then:

f( 1) = 1

f(2) = 2

Right n>2, there are:

F(n) = (number of methods with one grid first and then n- 1 grid)+(number of methods with two grids first and then n-2 grid)

that is

f(n) = f(n- 1) + f(n-2)

So f(n) is the n+ 1 term of Fibonacci sequence?

# include & ltstdio.h & gt

Long fiber (integer)

{if (n == 1 || n == 2) returns1;

Return fiber (n-1)+fiber (n-2);

}

Master ()

{ int n;

scanf("%d ",& ampn);

printf("%ld\n ",fib(n+ 1));

Returns 0;

}

/*

Exercise 9: Go up the stairs ★★

To go up the stairs, you can go up the stairs one by one, or you can come two by two.

You can also take a step or two, or even a k-step.

The problem is that if the number of last steps is n, each step can go up to 1 to k,

How many roads did you walk?

If n = 5 and k = 2, yes.

1, 1, 1, 1, 1

2, 1, 1, 1

1,2, 1, 1

1, 1,2, 1

1, 1, 1,2

2,2, 1

2, 1,2

1,2,2

There are eight kinds.

Enter the number of steps n (1

5 2

5 3

Output:

eight

13

Difficulty: easy

*/

# include & ltstdio.h & gt

# include & ltstring.h & gt

int d[3 1][3 1];

int dfs(int left,int k)

{

int I;

if(d[left][k]! =- 1) returns d [left] [k];

If (! Left) Return to1;

d[left][k]= 0;

for(I = 1; I < = ((left <; k)? Left: k); i++)

d[left][k]+=dfs(left-i,k);

Return d[ left] [k];

}

int main(void)

{

int n,k;

memset(d,- 1,sizeof(d));

while(scanf("%d%d ",& ampn & amp; k)! =EOF)

printf("%d\n ",dfs(n,k));

Returns 0;

}