"Codeforces 312B" bupt Newbie practice #3A Archer

Source: Internet
Author: User

Problem

Smallr is an archer. Smallr is taking a match of Archer with Zanoes. They try to shoot in the target in turns, and Smallr shoots first. The probability of shooting the target is SMALLR and for zanoes. The one who shoots in the target first should is the winner.

Output the probability that Smallr would win the match.

Input

A single line contains four integers.

Output

Print a single real number, the probability that Smallr would win the match.

The answer would be considered correct if the absolute or relative error doesn ' t exceed -6.

Sample Test (s) input
1 2 1 2
Output
0.666666666667

Test instructions: Give the probability (score) of the target of two archers, the two take turns shooting, the probability of winning the first Archer (decimal)

Analysis: The first archer wins, then the first shot or, the first time not the second archer is not in, the second time, ...

So the desired = first shot probability + (first bit probability * second not medium probability) * First shot probability + (first position not medium probability * second position not medium probability) * (first position not medium probability) * First shot probability +......= first shot probability * (1+ (First chance * The second not the middle probability) + (first not the probability * second not the middle probability) ²+ (first not the probability * second position not the probability) ³+ ... )

The problem requires less than 1e-6 error, just at the beginning my code is the following

#include <stdio.h>int main () {    double a,b,c,d,ans,u;    scanf ("%lf%lf%lf%lf", &a,&b,&c,&d);    ans=1;u=1;    while (u*a/b>=1e-6) {//change to-7,-8 also wa,-9 can be        u*= (1-a/b) * (1-C/D);        ans+=u;    }    printf ("%lf\n", ans*a/b);    return 0;}

Because ANS continues to add when u*a/b<1e-6, it may be more than 1e-6 than the current ans, because it is added several times; a better solution is to use the geometric series summation formula

s= (1-QN)/(1-Q), when n tends to infinity, because 0<q<1, so s=1/(1-Q)

q= (1-a/b) * (1-C/D), ans=s*a/b.

#include <stdio.h>int main () {    double a,b,c,d,ans,u;    scanf ("%lf%lf%lf%lf", &a,&b,&c,&d);    ANS=1/(1-a/b) * (1-C/D));    printf ("%.12lf\n", ans*a/b);    return 0;}

Make it simple, get it.

#include <stdio.h>int main () {    double a,b,c,d,ans,u;    scanf ("%lf%lf%lf%lf", &a,&b,&c,&d);    printf ("%.12lf\n", a*d/(A*d+b*c-a*c));    return 0;}

"Codeforces 312B" bupt Newbie practice #3A Archer

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.