LightOJ-1027 A Dangerous Maze Probability

Source: Internet
Author: User

LightOJ-1027 A Dangerous Maze Probability

There are n doors in the maze, and each door has a corresponding value. Suppose the value of the I-th door is xi. If xi is greater than 0, after xi minutes, you can leave this door to walk out of the maze. If xi <0, it indicates that after walking this door, it will take abs (xi) minutes before returning to the origin, what are the expectations of getting out of the maze?

Solution: Assume that there are k doors (positive values are represented by x, negative values are represented by y) and d is expected.
Then d = 1/k * (x1 + x2 + x3 + .. xn) + 1/k * (abs (y1) + abs (y2) +... + Abs (ym) + m * d)
It indicates that the probability of 1/k is selected to any door. It takes x minutes to go to the right door. after going to the main door, you can directly go out. Therefore, the expected value is 1/k * x.
If it is a negative number, it takes y minutes to return to the origin point after y minutes. Therefore, the expectation is 1/k * (y + d)

#include
  
   #include
   
    #include
    
     using namespace std;const int N = 110;const double esp = 1e-5;int num[N];int n, cnt;int gcd(int a, int b) {    return a == 0 ? b :  gcd(b % a, a);}void solve() {    int t = 0;    for(int i = 0; i < n; i++)        t = t + (num[i] > 0 ? num[i] : -num[i]);    int g = gcd((n - cnt), t);    printf("%d/%d\n", t  / g, (n - cnt) / g);}int main() {    int test, cas = 1;    scanf("%d", &test);    while(test--) {        scanf("%d", &n);        cnt = 0;        for(int i = 0; i < n; i++) {            scanf("%d", &num[i]);            if(num[i] < 0)                cnt++;        }        printf("Case %d: ", cas++);        if(cnt == n) {            printf("inf\n");            continue;        }        solve();    }    return 0;}
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.