P1255 数楼梯

题目链接:P1255

解题思路:

倒推,最后上楼梯的方法数等于f(n) = f(n-1) + f(n-2),用递推+高精

AC代码:

 

 1 #include <iostream>
 2 #define mx 1212 // 固定位数
 3 using namespace std;
 4 int a[5010][5010],f;
 5 int main()
 6 {
 7     f = 0;
 8     a[1][0] = 1; a[2][0] = 2; // 初始计算出前两个
 9     for(int i = 3; i < 5010; i++) // 离线计算
10     {
11         if(i == 5009) break;
12         for(int j = 0; j < mx; j++) // 高精度加法
13         {
14             a[i][j] += a[i-1][j]+a[i-2][j];
15             if(a[i][j] >= 10)
16             {
17                 a[i][j+1] = a[i][j+1]+a[i][j]/10;
18                 a[i][j] %= 10;
19             }
20         }
21     }
22     int n;
23     cin >> n;
24     if(n == 0) {cout << 0; return 0;}
25     for(int i = mx; i >= 0; i--)
26     {
27         if(a[n][i] == 0 && f == 0) continue; // 输出除去前导零
28         else {cout << a[n][i]; f = 1;}
29     }
30     return 0;
31 }

 

posted @ 2020-09-08 13:45  不敢说的梦  阅读(342)  评论(0)    收藏  举报