zoukankan      html  css  js  c++  java
  • A Dangerous Maze (概率期望)

    A Dangerous Maze

     LightOJ - 1027 

    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 xi minutes. 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 nspace separated integers. If the ith 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

    3

    1

    1

    2

    -10 -3

    3

    3 -6 -9

    Sample Output

    Case 1: 1/1

    Case 2: inf

    Case 3: 18/1

    题意:在n个门前选择一扇门出去, 然后如果第i扇门的 Xi值是正的话,你会花费Xi时间后出去 ,如果Xi是负数的话你会花费Xi时间后回到老地方,并且忘记了刚才的选择,选择一扇门的概率是等概的。求出去的期望。

    题解:定义一次选择选择到Xi是整数的概率为P1,选择到负数的概率是P2,然后选择了正数后平均在T1时间后出去, 选择了负数后平均在T2时间后回到原地。接着设出去的期望是Y,那么可以写出一个式子 :Y = P1 * T1 + P2 * (T2 + Y)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int a[110];
     7 int n;
     8 int time1,time2;
     9 int gcd(int a,int b)
    10 {
    11     if(b==0)
    12         return a;
    13     return gcd(b,a%b);
    14 }
    15 int main()
    16 {
    17     int casen,ca=1;
    18     scanf("%d",&casen);
    19     while(casen--)
    20     {
    21         int flag=0;
    22         time1=0,time2=0;
    23         int sum1=0,sum2=0;
    24         scanf("%d",&n);
    25         for(int i=1;i<=n;i++)
    26         {
    27             scanf("%d",&a[i]);
    28             if(a[i]>0)
    29             {
    30                 flag=1;
    31                 time1+=a[i];
    32                 sum1++;
    33             }
    34             else      
    35             {
    36                 time2+=(-a[i]);
    37                 sum2++;
    38             }
    39         }    
    40         printf("Case %d: ",ca++);
    41         if(!flag)
    42         {
    43             printf("inf
    ");
    44             continue;
    45         }
    46         else
    47         {
    48             int up=time1+time2;
    49             int down=n-sum2; 
    50             int g=gcd(up,down);
    51             printf("%d/%d
    ",up/g,down/g);
    52         }
    53     }
    54 } 
  • 相关阅读:
    部署至Oracle数据库的注意事项
    当在centos上面部署项目时,mysql的一些驱动安装不上,
    drf利用redis做缓存是发生一点错误提示
    python字符串前面加个u代表什么
    在学习数据分析时,安装anaconda时遇到一点问题
    在使用scrapy框架爬取sina时,常见保存
    在linux上scrapyd部署scrapy项目时出现service_identity,并且不能识别其中的一个opentype模块
    如何在mysql增加一个和root权限一样的用户,安排
    mysq的root密码忘记,原来还有更优雅的解法
    mysql远程连接阿里云的Ubuntu服务器
  • 原文地址:https://www.cnblogs.com/1013star/p/10361561.html
Copyright © 2011-2022 走看看