• Uva 10081 Tight words (概率DP)


    Time limit: 3.000 seconds

    Given is an alphabet {0, 1, ... , k}0 <= k <= 9 . We say that a word of length n over this alphabet is tightif any two neighbour digits in the word do not differ by more than 1.

    Input is a sequence of lines, each line contains two integer numbers k and n1 <= n <= 100. For each line of input, output the percentage of tight words of length n over the alphabet {0, 1, ... , k} with 5 fractional digits.

    Sample input

    4 1
    2 5
    3 5
    8 7
    

    Output for the sample input

    100.00000
    40.74074
    17.38281
    0.10130

    题意:给定两个数k,n。

     {0, 1, ... , k}的数组成一个n个数的序列。假设这个序

    列每两个相邻的数相差<=1,就记为是tight,求这样的序列占总序列的比率。


    思路: dp[i][j]表示第i为数字是j的概率 。  

       即   dp[i][j] = 1/(k+1) *  (dp[i-1][j-1]+dp[i-1][j] + dp[i+1][j] ); 

        注意下边界就OK了。


    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int maxn=105;
    
    double dp[maxn][15],t,ans;
    int n,k;
    
    void initial()
    {
        memset(dp,0,sizeof(dp));
        t=1.0/(k+1),ans=0.0;
        for(int j=0; j<=k; j++)   dp[1][j]=t;
    }
    
    void solve()
    {
        for(int i=2; i<=n; i++)
            for(int j=0; j<=k; j++)
            {
                dp[i][j]+=t*dp[i-1][j];
                if(j!=0)  dp[i][j]+=t*dp[i-1][j-1];
                if(j!=k)  dp[i][j]+=t*dp[i-1][j+1];
            }
        for(int j=0; j<=k; j++) ans+=dp[n][j];
        printf("%.5lf
    ",ans*100);
    }
    
    int main()
    {
        while(scanf("%d %d",&k,&n)!=EOF)
        {
            initial();
            solve();
        }
        return 0;
    }
    



  • 相关阅读:
    猜数字游戏,猜三次都不对,则结束游戏,猜一次就成功,结束游戏
    用return关键字实现1——100累加求和,返回总和并接收输出
    用return关键字实现求和操作
    return关键字的作用和接受实验
    数组各元素随机赋值、求和、求平均值、求最大值的各类测试(一维数组)
    日期下拉选择
    22--
    css 17课--
    css盒模型
    css学习
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6934490.html
Copyright © 2020-2023  润新知