light oj 1027(概率+期望)

这是一个关于迷宫逃脱问题的编程题,你面对n扇门,每扇门有不同概率带你离开或回到起点。你需要计算出从迷宫中逃脱的预期时间。输入包含测试用例数和每扇门的信息,输出为预期时间(分子/分母形式)。当所有门都通向起点时,输出'inf'。题目涉及概率论中的期望值计算。
摘要由CSDN通过智能技术生成

1027 - A Dangerous Maze
Time Limit: 2 second(s)Memory Limit: 32 MB

You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.

If you choose the ith door, it can either take you back to the same position where you begun in xi minutes, or can take you out of the maze after ximinutes. If you come back to the same position, you can't remember anything. So, every time you come to the beginning position, you have no past experience.

Now you want to find the expected time to get out of the maze.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of doors. The next line contains n space separated integers. If theith integer (xi) is positive, you can assume that the ith door will take you out of maze after xi minutes. If it's negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.

Output

For each case, print the case number and the expected time to get out of the maze. If it's impossible to get out of the maze, print 'inf'. Print the result in p/q format. Where p is the numerator of the result and q is the denominator of the result and they are relatively prime. See the samples for details.

Sample Input

Output for Sample Input

3

 

1

1

 

2

-10 -3

 

3

3 -6 -9

Case 1: 1/1

Case 2: inf

Case 3: 18/1




有点类似于解方程 设未知数的思想。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 110;
int gcd(int x,int y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}


int main()
{
    int t, n, x, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        int cnt=0, sum=0;
        scanf("%d", &n);
        for(int i=0;i<n;i++)
        {
            scanf("%d", &x);
            if(x>=0)
                sum+=x;
            else
                sum-=x,cnt++;
        }
        if(cnt==n)
            printf("Case %d: inf\n",ncase++);
        else
            printf("Case %d: %d/%d\n",ncase++, sum/gcd(sum,n-cnt),(n-cnt)/gcd(sum,n-cnt));
    }
    return 0;
}

定义1:

按照定义,离散随机变量的一切可能值与其对应的概率P的乘积之和称为数学期望,记为咐.如果随机变量只取得有限个值:x,、π

定义2:

决定可靠性的因素常规的安全系数是根据经验而选取的,即取材料的强度极限均值(概率理论中称为数学期望)与工作应力均值(数学期望)之比。

随机变量的数学期望值

在概率论和统计学中,一个 离散性随机变量的期望值(或数学期望、或均值,亦简称期望)是试验中每次可能结果的概率乘以其结果的总和。换句话说,期望值是随机试验在同样的机会下重复多次的结果计算出的等同“期望”的平均值。需要注意的是,期望值并不一定等同于常识中的“期望”——“期望值”也许与每一个结果都不相等。(换句话说,期望值是该变量输出值的平均数。期望值并不一定包含于变量的输出值集合里。)

单独数据的数学期望值算法

对于数学期望的定义是这样的。数学期望

E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn)

X1,X2,X3,……,Xn为这几个数据,p(X1),p(X2),p(X3),……p(Xn)为这几个数据的概率函数。在随机出现的几个数据中p(X1),p(X2),p(X3),……p(Xn)概率函数就理解为数据X1,X2,X3,……,Xn出现的频率f(Xi).则:

E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) = X1*f1(X1) + X2*f2(X2) + …… + Xn*fn(Xn)

很容易证明E(X)对于这几个数据来说就是他们的算术平均值

我们举个例子,比如说有这么几个数:

1,1,2,5,2,6,5,8,9,4,8,1

1出现的次数为3次,占所有数据出现次数的3/12,这个3/12就是1所对应的频率。同理,可以计算出f(2) = 2/12,f(5) = 2/12 , f(6) = 1/12 , f(8) = 2/12 , f(9) = 1/12 , f(4) = 1/12 根据数学期望的定义:

E(X) =1*f(1)+ 2*f(2) + 5*f(5) + 6*f(6) + 8*f(8) + 9*f(9) + 4*f(4) = 13/3

所以 E(X) = 13/3,

现在算这些数的算术平均值:

Xa = (1+1+2+5+2+6+5+8+9+4+8+1)/12 = 13/3

所以E(X) = Xa = 13/3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值