zoukankan      html  css  js  c++  java
  • ACdream 1113 The Arrow (概率dp求期望)

    E - The Arrow
    Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    The history shows that We need heroes in every dynasty. For example, Liangshan Heroes. People hope that these heroes can punish the bad  guys and recover the justice. Nowadays, we also need heroes to provent us from being chopped, or being attacked by a bomb. 

    Kuangbin is a very very very very very.... (very * 1e9 ) good boy, after he knows The Arrow, he want to be The Arrow of China. But he is also a little afraid of being killed by the bad guys. So he decides to throw dices to make the decision.

    The dice is a cube with 1 2 3 4 5 6 on it's sides. When he throws a dice, every number is of the same probablity to appear. He will write down a number N in the paper at first, and then throw the dice. When the sum of the number he throwed is less than N, he will keep throwing. But if the sum exceeds N, this throwing does not count.

    For example, If the sum is 5,and N is 6, if we throw 2, 5 + 2 > 6, then  the sum keeps to be 5.

    If he can end the throwing in a certain time, he will make the decision to become The Arrow.

    Now , kuangbin wonders that what's the expectation of the time of throwing dices.

    Input

    First line, The number of cases t <= 100

    In the next t lines, there will be t numbers.

    every number is not bigger than 100000

    Output

    Each test output a number rounded to the second digit.

    Sample Input

    1
    1

    Sample Output

    6.00
    题意:给定一个数n。现有一个骰子,六个面分别是123456,掷骰子,记录下掷出来的数的总和以及掷的次数,求和为n的时候,掷的次数的期望为多少。如果掷的和超过n就保持原来的n,比如n=10,第一次掷5第二次掷6,和是11>10,n保持5不变。有T组数据,每组数据给定一个n。
    题解:掷到123456的期望是6,掷到7的期望是掷到123456的期望总和除以6+1。给出优化前和优化后的代码:
    优化前:
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    int main()
    {
        int t;
        double dp[101005];
        cin>>t;
        while(t--)
        {
            int n;
            cin>>n;
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=6;i++)
                dp[i]=6;
            for(int i=7;i<=n;i++)
            {
                dp[i]++; //又掷了一次
                for(int j=1;j<=6;j++)
                    dp[i]=dp[i]+dp[i-j]/6;
            }
            printf("%.2lf
    ",dp[n]);
        }
        return 0;
    }

    优化后:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int maxn=1e5+5;
    double dp[maxn];
    void get()
    {
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=6; i++)
            dp[i]=6;
        for(int i=7; i<=maxn; i++)
            dp[i]=dp[i-1]-dp[i-7]/6+dp[i-1]/6;
    }
    int main()
    {
        get();
        int t;
        cin>>t;
        while(t--)
        {
            int n;
            cin>>n;
            printf("%.2lf
    ",dp[n]);
        }
        return 0;
    }
     
  • 相关阅读:
    C#生成指定范围内的不重复随机数
    jquery移除元素某个属性
    让html里的js脚本延迟5秒运行
    jquery刷新局部和全页的方法
    jquery旋转插件rotate参数说明
    按照日期生成编号
    ajax请求成功后如何调用bootstrap modal?
    WPF MVVM模式不用Prism
    C#去掉字符串两端空格以及去掉字符串中多余空格保留一个空格
    依赖项属性
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5440268.html
Copyright © 2011-2022 走看看