2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A题 Weather Patterns

2017-09-25 15:49:45

writer:pprp

阅读理解,当时没有耐心去读,只要能读懂就大概可以做出来

题意如下:

有四种天气,

State 1: snow

State 2: rain

State 3: cloudy

State 4: sunny

给你一个4*4矩阵代表aij 代表从状态i转化为状态j的概率,其中第一天的概率为1

给你一串序列,问你从第一个到最后一个的概率

给你一个数字,问你该状态的数学期望是多少?

E(x)  =  1 + 2 * p + 3 * p * p + ..... 

精度为1e-8,但是实际上要精确到1e-10

代码如下:

复制代码
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
//    freopen("in.txt","r",stdin);
    double mtx[5][5];
    double ans = 1;
    int neo;

    for(int i = 1; i <= 4; i++)
        for(int j = 1; j <= 4; j++)
            scanf("%lf",&mtx[i][j]);
    getchar();
    int pre, nex;
    pre = -1;
    while(1)
    {
        scanf("%d",&neo);
        char ch = getchar();

        if(pre == -1)
        {
            pre = neo;
        }
        else
        {
            nex = neo;
            ans *= mtx[pre][nex];
            pre = neo;
        }
        if(ch == '\n')
            break;
    }
    printf("%.8lf\n",ans);
    pre = -1;
    ans = 1;

    while(1)
    {
        scanf("%d",&neo);
        char ch = getchar();

        if(pre == -1)
        {
            pre = neo;
        }
        else
        {
            nex = neo;
            ans *= mtx[pre][nex];
            pre = neo;
        }
        if(ch == '\n')
            break;
    }
    printf("%.8lf\n",ans);

    int rec;
    scanf("%d",&rec);
    double pprp = mtx[rec][rec];
    ans = 1;
    double tmp = pprp;
    for(int i = 2;; i++)
    {
        double j = i*tmp;
        if(j < 1e-10)
            break;
        ans += j;
        tmp *= pprp;
    }
    printf("%.8lf\n",ans*(1-pprp));

    scanf("%d",&rec);
    pprp = mtx[rec][rec];
    ans = 1;
    tmp = pprp;
    for(int i = 2;; i++)
    {
        double j = i*tmp;
        if(j < 1e-10)
            break;
        ans += j;
        tmp *= pprp;
    }
    printf("%.8lf\n",ans*(1-pprp));
    return 0;
}
复制代码

 

posted @   pprp  阅读(338)  评论(0编辑  收藏  举报
编辑推荐:
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 用 C# 插值字符串处理器写一个 sscanf
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示