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;
}
代码改变世界