• poj3682 King Arthur's Birthday Celebration 数学期望


    Description

    King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. The luxurious celebration will start on his birthday and King Arthur decides to let fate tell when to stop it. Every day he will toss a coin which has probability p that it comes up heads and 1-p up tails. The celebration will be on going until the coin has come up heads for K times. Moreover, the king also decides to spend 1 thousand coins on the first day's celebration, 3 thousand coins on the second day's, 5 thousand coins on the third day's ... The cost of next day will always be 2 thousand coins more than the previous one's. Can you tell the minister how many days the celebration is expected to last and how many coins the celebration is expected to cost?

    Input

    The input consists of several test cases.
    For every case, there is a line with an integer K ( 0 < K ≤ 1000 ) and a real number p (0.1 ≤ p ≤ 1).
    Input ends with a single zero.

    Output

    For each case, print two number -- the expected number of days and the expected number of coins (in thousand), with the fraction rounded to 3 decimal places.

    Sample Input

    1 1
    1 0.5
    0

    Sample Output

    1.000 1.000
    2.000 6.000

    题解


    我自己用了一种贼毒瘤的操作,但它wa了,并且思路应该是没问题的,留坑

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iomanip>
    #include<cmath>
    #define f(x) (1.0*(x)/p)
    using namespace std;
    double g[1010];
    int main(){
        ios::sync_with_stdio(false);
        int k;
        double p;
        // freopen("1.in","r",stdin);
        //freopen("1.out","w",stdout);
        while(1){
            cin>>k;if(k==0)break;
            cin>>p;
            cout.setf(ios::fixed);
            cout<<fixed<<setprecision(3)<<f(k)<<" ";
            for(int i=1;i<=k;i++)
               g[i]=1.0*g[i-1]+2.0*f(i-1)+1.0/p+2.0/p*f(i)-2*f(i);
            cout<<fixed<<setprecision(3)<<g[k]<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    linux crontab 定时使用方法
    crontab 选择编辑器 select-editor
    设置定时任务为每天凌晨2点执行和每小时执行一次
    性能测试工具--SIEGE安装及使用简介 siege压力测试
    Vue基础
    使用 supervisor 管理进程
    长按listview弹出选项列表对话框
    左右滑动弹窗之间短信内容区域来显示上一条和下一条短信
    在开机广播中启动服务
    Android spinner 样式及其使用详解
  • 原文地址:https://www.cnblogs.com/Nan-Cheng/p/9884077.html
Copyright © 2020-2023  润新知