poj1671

题意:一首n行诗,每一行有一种韵律,问这首诗总共可能有多少种韵律排列

思路:dp or 递推

     f[i][j]表示前i个位置,用j种韵律的方案数

    f[i][j] = f[i-1][j]*j + f[i-1][j-1]

   {其实挺好理解的,如果没出现新的韵律,那么就用前面的j种任意一种韵律来填充,否则就新引入一种韵律}

 1 /*
 2   State:Accpeted
 3   Time:2013-03-07 22:02:23
 4 */
 5 #include <iostream>
 6 #include <cstring>
 7 #include <string>
 8 #include <cstdlib>
 9 #include <cstdio>
10 #include <algorithm>
11 #include <cmath>
12 using namespace std;
13 const int maxn = 100;
14 double f[maxn + 1][maxn + 1] , ans[maxn + 1];
15 int n;
16 int main(){
17     freopen("poj1671.in","r",stdin);
18     freopen("poj1671.out","w",stdout);
19     for (int i = 1 ;  i <= maxn; ++i){
20          f[i][1]  =  1;
21          ans[i] = 1;
22     }
23     for (int i = 1 ; i <= maxn; ++i)
24         for (int j = 2;  j <= i; ++j){
25            f[i][j] = f[i - 1][j - 1] + f[i - 1][j] * j;
26            ans[i] += f[i][j];
27         }
28     while (1){
29            scanf("%d",&n);
30            if (!n) break;
31            printf("%d %.0f\n", n , ans[n]);
32     }
33     fclose(stdin); fclose(stdout);
34 }

 

 

       

posted on 2013-03-23 21:41  yzcstc  阅读(203)  评论(0编辑  收藏  举报