【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) ...
随机推荐
- 优化后的二次测试Miller_Rabin素性测试算法
ll random(ll n) { return (ll)((double)rand()/RAND_MAX*n + 0.5); } ll pow_mod(ll a,ll p,ll n) { ) ; l ...
- js 日期2015/12/22/16/45替换2015-12-22 16:45格式
js 日期2015/12/22/16/45替换2015-12-22 16:45格式 利用正则分组: function toChange(date) { var reg = /(\d+)\/(\d+)\ ...
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(安装ubuntu虚拟机)-(5)
之前介绍了在webvirtmgr平台下创建centos,windows server 2008的虚拟机,今天说下创建ubuntu虚拟机的过程. (1)首先下载ubuntu16.04的iso镜像放到/u ...
- 【C#】IDispose接口的应用
.net的GC机制有两个问题: 一.GC并不能释放所有资源,它更不能释放非托管资源. 二.GC也不是实时的,所有GC存在不确定性.所以需要使用析构函数,但是为了不重复GC,需要做一些处理. publi ...
- 携手K2 BPM,华住酒店完美实现“互联网+”转型
华住酒店集团,旗下6大品牌酒店,包括商旅品牌—禧玥酒店.全季酒店.星程酒店.汉庭酒店.海友酒店,以及度假品牌—漫心度假酒店.高端大气上档次一气呵成,2013年签约K2,携手成就美好生活. 演讲人:宋洪 ...
- Linux Linux程序练习十一(网络编程大文件发送UDP版)
//网络编程发送端--大文件传输(UDP) #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
- sudo权限添加 和 rpm、deb之名词解释
sudo权限添加: 刚开始用Center_os Linux操作系统,想装个输入法,搜了一下,看到linux下的搜狗输入法(帖子链接)下载下来的文件的扩展名是.deb,直接用帖子上的一个命令: sudo ...
- (转载)关于Apache 的两种工作模式
今天在查看服务器的时候,发现服务器http请求数 每天增长越来越多,在优化集群服务器的时候,查看到Apache 的工作模式是prefork,于是想到了worker 模式, 想暂时的把当前运行模式改成w ...
- python数字图像处理(13):基本形态学滤波
对图像进行形态学变换.变换对象一般为灰度图或二值图,功能函数放在morphology子模块内. 1.膨胀(dilation) 原理:一般对二值图像进行操作.找到像素值为1的点,将它的邻近像素点都设置成 ...
- 在matlab中将处理结果输出为shp文件
在matlab中读入shp文件很简单,一个函数shaperead就可以了,但输出为shp文件就稍微麻烦一些了.shp文件实际上就是一个struct,因此得到处理结果后,要先将数据变成struct结构, ...