High precision: counting stairs

Title description

There are N steps in the stairs. You can go upstairs one step or two steps.

Write a program to calculate how many different moves there are.

Input format

A number, the number of stairs.

Output format

There are several ways to walk.

Sample input and output

Enter # 1 
4
Output # 1 
5

Instructions / Tips

60% N<=50
100% N<=5000)

 

Idea: At the beginning, I wrote 40 points myself. I didn't expect there is a high-precision part. I looked at the solution and used a two-dimensional array to save it. If the current value is greater than 10, I carry it and output it in reverse order.

1 #include <iostream>
 2 #include <algorithm>
 3  
4  using  namespace std;
 5  
6  int n, len = 1 , f [ 5003 ] [ 5003 ]; // f [k] [i] corresponds to the kth step The number of moves is 
7  void hp ( int k)
 8  {
 9      int i;
 10  
11      for (i = 1 ; i <= len; i ++ )
 12      {
 13          f [k] [i] = f [k- 1 ] [ i] + f [k- 2 ] [i];
 14     }
15 
16     for (i = 1; i <= len; i++)
17     {
18         if (f[k][i] >= 10)
19         {
20             f[k][i + 1] += f[k][i] / 10;
21             f[k][i] = f[k][i] % 10;
22             if (f[k][len + 1])
23             {
24                 len++;
25             }
26         }
27     }
28 }
29  
30  int main ()
 31  {
 32      int i;
 33  
34      cin >> n;
 35  
36      f [ 1 ] [ 1 ] = 1 ;
 37      f [ 2 ] [ 1 ] = 2 ;
 38      for (i = 3 ; i <= n; i ++) // Starting from the third order 
39      {
 40          hp (i);
 41      }
 42      for (i = len; i> = 1 ; i-- )
 43      {
44         cout << f[n][i];
45     }
46     return 0;
47 }

 

Guess you like

Origin www.cnblogs.com/ZhengLijie/p/12719989.html