【CodeForces 312B】BUPT 2015 newbie practice #3A Archer
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.
A single line contains four integers .
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.
1 2 1 2
0.666666666667
题意:给出两个弓箭手的射中目标的概率(分数),两人轮流射击,求第一位弓箭手赢的概率(小数)
分析:第一位弓箭手赢,那么第一次就射中或者,第一次不中而第二个弓箭手也不中,第二次中,……
p=a/b为第一位射中的概率,q=c/d为第二位射中的概率。
题目要求误差小于1e-6,刚开始我的代码是下面这样
#include<stdio.h>
int main(){
double a,b,c,d,ans,u;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
ans=;u=;
while(u*a/b>=1e-){//改成-7、-8也会WA,-9可过
u*=(-a/b)*(-c/d);
ans+=u;
}
printf("%lf\n",ans*a/b);
return ;
}
因为当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.
#include<stdio.h>
int main(){
double a,b,c,d,ans,u;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
ans=/(-(-a/b)*(-c/d));
printf("%.12lf\n",ans*a/b);
return ;
}
进行化简一下得到
#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 ;
}
【CodeForces 312B】BUPT 2015 newbie practice #3A Archer的更多相关文章
- 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...
- 【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 ...
- 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...
- 【UVA 401】BUPT 2015 newbie practice #2 div2-B-Palindromes
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/B A regular palindrome is a str ...
- 【UVA 11078】BUPT 2015 newbie practice #2 div2-A -Open Credit System
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/A In an open credit system, the ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【最大流】ECNA 2015 F Transportation Delegation (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: N(N<=600)个点,每个点有个名字Si,R(R<=200)个生产商在R个点上,F(F<= ...
- 【宽搜】ECNA 2015 D Rings (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: 给你一张N*N(N<=100)的图表示一个树桩,'T'为年轮,'.'为空,求每个'T'属于哪一圈年轮,空 ...
- 【宽搜】ECNA 2015 E Squawk Virus (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: N个点M条无向边,(N<=100,M<=N(N-1)/2),起始感染源S,时间T(T<10) ...
随机推荐
- C# Reflection Type/MethodInfo
C#反射 在C#的反射中,可以通过Type来执行类中的某个方法,也可以通过MethodInfo来执行方法 三种调用方法 下面的示例中使用了三种方法来执行方法 两个类:Class1和Demo1,通过反射 ...
- Unity物理系统的触发器
如何触发触发器函数? 触发器中相互的,当其中一个是触发器,两个物体进入碰撞,双方的触发器函数都会触发. 两个碰撞盒穿入? 解决办法:给其中一个添加刚体 触发器的物理配置 以上是个人理解,看过API之后 ...
- eclipse android 不会自动生成R.java文件和包的解决办法
eclipse下编写android程序突然不会自动生成R.java文件和包的解决办法 我的eclipse原来是好好的,什么问题都没有的,结果今天一打开新建一个android程序,发现工程里不会自动 ...
- android窗口泄漏,isInEditMode解决可视化编辑器无法识别自定义控件的问题
android窗口泄漏 在做项目是遇到这个错误:google:WindowManager: Activity has leaked window. 产 生原因:我们知道Android的每一个Activ ...
- 后台首页品字形(frameset)框架搭建
get_defined_constants([true])//显示所有常量信息.参数true,表示分组显示,查看当前系统给我提供了哪些常量可以使用,包括自定义常量. __CONTROLLER__//获 ...
- 未能加载文件或程序集“Enyim.Caching”或它的某一个依赖项。未能验证强名称签名
from:http://www.mzwu.com/article.asp?id=3741 itHub下载Enyim项目,编译后引用程序运行出错: 引用内容 未能加载文件或程序集“Enyim.Cachi ...
- Redis集群环境的部署记录
Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩. 一. 集群简单概念. Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施( ...
- localStorage和sessionStorage区别
localStorage和sessionStorage一样都是用来存储客户端临时信息的对象. 他们均只能存储字符串类型的对象(虽然规范中可以存储其他原生类型的对象,但是目前为止没有浏览器对其进行实现) ...
- WinForm 快捷键设置
一.窗体快捷键,只在窗体上有效果 首先在form_load的时候写上this.KeyPreview=true;//表示窗体接受按键事件 然后如下 private void Frm_KeyDown(ob ...
- C语言 读取文件中特定数据
//读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...