Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces 312B】BUPT 2015 newbie practice #3A Archer

【CodeForces 312B】BUPT 2015 newbie practice #3A Archer

作者头像
饶文津
发布于 2020-05-31 15:37:53
发布于 2020-05-31 15:37:53
48500
代码可运行
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏
运行总次数:0
代码可运行

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 each time is 

 for SmallR while 

 for Zanoes. The one who shoots in the target first should be the winner.

Output the probability that SmallR will win the match.

Input

A single line contains four integers 

.

Output

Print a single real number, the probability that SmallR will win the match.

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

Sample test(s)

input

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1 2 1 2

output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
0.666666666667

 题意:给出两个弓箭手的射中目标的概率(分数),两人轮流射击,求第一位弓箭手赢的概率(小数)

分析:第一位弓箭手赢,那么第一次就射中或者,第一次不中而第二个弓箭手也不中,第二次中,……

p=a/b为第一位射中的概率,q=c/d为第二位射中的概率。

题目要求误差小于1e-6,刚开始我的代码是下面这样

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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){//改成-7、-8也会WA,-9可过
        u*=(1-a/b)*(1-c/d);
        ans+=u;
    }
    printf("%lf\n",ans*a/b);
    return 0;
}

 因为当u*a/b<1e-6时ans继续加下去,可能比当前ans大不止1e-6,因为后面加了好几次;正确的方法是用等比数列求和公式

s=(1-qn)/(1-q),当n趋于无穷时,因为0<q<1,所以s=1/(1-q)

q=(1-a/b)*(1-c/d),ans=s*a/b.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<stdio.h>
int main(){
    double a,b,c,d,ans,u;
    scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
    ans=1/(1-(1-a/b)*(1-c/d));
    printf("%.12lf\n",ans*a/b);
    return 0;
}

进行化简一下得到

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-02-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
codeforces 312
A. Whose sentence is it? 代码: //codeforces 312 A //2013-05-01-19.12 #include <stdio.h> #include <stri
xindoo
2021/01/22
2440
SP4060 KPGAME - A game with probability
有n个石子在这里,Alice和Bob轮流投掷硬币,如果正面朝上,则从n个石子中取出一个石子,否则不做任何事。取到最后一颗石子的人胜利。Alice在投掷硬币时有p的概率投掷出他想投的一面,Bob有q的概率投掷出他相投的一面。
yzxoi
2022/09/19
4330
Codeforces Round #327 div2
  有一段长度为l的路,两个人分别在两个端点,1, l。 现在已知每个人的速度为p,q. 求第一个人(初始位置在1)在他们第二次相遇的时候的位置。
熙世
2019/07/14
3540
LightOj_1408 Batting Practice
  击球训练中, 你击中一个球的概率为p,连续击中k1个球, 或者连续击空k2个球, 则训练结束。
熙世
2019/07/15
3680
【概率期望动态规划】
1~8题出处:hdu4576   poj2096   zoj3329   poj3744   hdu4089   hdu4035   hdu4405   hdu4418
attack
2018/09/17
1K0
【概率期望动态规划】
Codeforces Round #321 div2
好像前几场的题解忘记写了, Orz 状态太差, 平均出两题   都不好意思写了 , 连掉4场, 都要哭晕了。
熙世
2019/07/15
3370
【UVALive 3905】BUPT 2015 newbie practice #2 div2-D-3905 - Meteor
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/D The famous Korean internet co
饶文津
2020/05/31
3660
数据结构期末52道OnlineJudge机试题目考核-大二上
<!--more--> <center> <a href="https://s2.ax1x.com/2019/12/28/lmEb9K.png" class="highslide" onclick="return hs.expand(this,{slideshowGroup:'images'})"><img src="https://s2.ax1x.com/2019/12/28/lmEb9K.png" alt="lmEb9K.png" border="0" /></a></center> **所有题目全部在杭电OJ完成:**[http://acm.hdu.edu.cn/](http://acm.hdu.edu.cn/) ```c //1000 A + B Problem #include<iostream> using namespace std; int main(){ int a,b,sum; while(cin>>a>>b){ sum = 0; sum = a + b; cout<<sum<<endl; } } ``` ```c //1005 Number Sequence #include <iostream> int A,B; int f(int n){ if( n == 1 || n == 2){ return 1; } return (A*f(n-1)+B*f(n-2))%7; } int main(){ int n; while(scanf("%d%d%d",&A,&B,&n)!=EOF,A||B||n){ int a = f(n%49); printf("%d\n",a); } } ``` ```c //1008 Elevator #include<iostream> using namespace std; int main(){ int n,a[100],i,sum; while(cin>>n&&n!=0){ sum = 0; for(i=0;i<n;i++){ cin>>a[i]; } if(n==1){ sum = a[0]*6+5; } else{ sum = n*5+a[0]*6; for(i=0;i<n-1;i++){ if(a[i+1]>a[i]){ sum += (a[i+1]-a[i])*6; } else{ sum += (a[i]-a[i+1])*4; } } } cout<<sum<<endl; } } ``` ```c //1012 u Calculate e #include<stdio.h> int main() { double e; int i, tmp; e = 1; tmp = 1; printf("n e\n"); printf("- -----------\n"); printf("0 1\n"); for(i = 1; i < 10; i++) { tmp *= i; e += 1.0/tmp; if(i>=3) printf("%d %.9lf\n", i, e); else if(2 == i) printf("%d %.1lf\n", i, e); else printf("%d %.0lf\n",i, e); } } ``` ```c //1032 The 3n + 1 problem #include <iostream> using namespace std; int main() { int a,b,t,i,max; while(cin >> a >> b) { cout << a << " " << b << " "; if(a>b)//大小不确定 { t = a; a = b; b = t; } max = 0;
王荣胜
2020/03/12
7030
Codeforces Beta Round #2 A,B,C
A. Winner time limit per test:1 second memory limit per test:64 megabytes input:standard input output:standard output The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game ther
Angel_Kitty
2018/04/08
1.1K0
计蒜客2018 蓝桥杯省赛 B 组模拟赛(一)
1,结果填空:年龄 今天蒜头君带着花椰妹和朋友们一起聚会,当朋友们问起年龄的时候,蒜头君打了一个哑谜(毕竟年龄是女孩子的隐私)说:“我的年龄是花椰妹年龄个位数和十位数之和的二倍”。 花椰妹看大家一脸懵逼,就知道大家也不知道蒜头君的年龄,便连忙补充道:“我的年龄是蒜头君个位数和十位数之和的三倍”。 请你计算:蒜头君和花椰妹年龄一共有多少种可能情况? 提醒:两位的年龄都是在 [10,100)[10,100) 这个区间内。 分析: 暴力枚举每一个人可能的年龄,然后判断是否符合条件。 #include<stdio.
Zoctopus
2018/06/04
1.1K0
Codeforces Round #360 div2
  有d天, n个人。如果这n个人同时出现, 那么你就赢不了他们所有的人, 除此之外, 你可以赢他们所有到场的人。
熙世
2019/07/14
3700
【Aizu 2305】Beautiful Currency
  给你n个货币价格,然后通过调整一些货币的大小,使得所有比自己小的货币都是该货币的约数,调整前第 i 货币为a,调整后为b 那么变化率为 ri=|a-b|/a ,总变化率为max(ri)。求最小的总变化率。
饶文津
2020/06/02
2860
Codeforces Round #315 (Div. 2)
这次可以说是最糟糕的一次比赛了吧, 心没有静下来好好的去思考, 导致没有做好能做的题。
熙世
2019/07/15
3580
Codeforces Round #361 div2
  有n个城市, 第i个城市到第j个城市需要消耗abs(i - j)的能量, 然后每个城市可以转移到另一个城市, 只需要一个能量, 转移是单向的。
熙世
2019/07/14
4700
如何提高编程能力?(中)
函数方程: y - f2 = (f2 - f1) / (x2 - x1)(x - x2) 化简得: x=(f2x1-f1x2)/(f2-f1)
公众号guangcity
2019/09/20
8400
老年人康复训练:基础篇
大概已经一年半没碰算法了,为找工作康复训练一下。 目标:落谷题单刷完-https://www.luogu.com.cn/training/list 按住Ctrl+F输入P+题号即可快速查看代码位置 ---- 入门1-6 速刷,不写题目描述和思路。 P5703 #include<stdio.h> int main() { long long a,b; scanf("%d%d",&a,&b); printf("%d",a*b); return 0; } P5704 #include<stdio.h>
[Sugar]
2022/09/21
4770
CodeForces 157B Trace
B. Trace time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This w
ShenduCC
2018/04/26
1.2K0
Codeforces Round #316 div2
  n个候选人在m个城市进行投票,每个城市选出票数最多的一个候选人为城市候选人,如果票数相同,则取编号小的候选人。
熙世
2019/07/15
3070
【PAT甲级】1002 A+B for Polynomials (25分)
This time, you are supposed to find A+B where A and B are two polynomials.
韩旭051
2020/07/21
8460
河工院首届工业设计大赛程序组(挑战赛)题解
本题主要考察四舍五入,C语言中是四舍六入,但是需要四舍五入,则在结果后面加上0.001即可。
浪漫主义狗
2024/05/07
1360
相关推荐
codeforces 312
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验