牛客-长安大学第三届ACM-ICPC程序设计竞赛-B-Happy Running

链接:https://www.nowcoder.com/acm/contest/102/B
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

Happy Running, an application for runners, is very popular in CHD. 
In order to lose weight, fdf decides to run k meters per day with Happy Running. Let’s regard the school as a circular runway with total length of x meters, and fdf could start running clockwise at a random point on the runway. Then Happy Running will randomly show two punching-card points (打卡点). Because of the poor physical strength, fdf decides to finish running only by completing the punch, whether or not complete the goal of k meters.
One day, before running, fdf wants to know whether he can achieve the goal today, and he asks you to help him to calculate the probability of completing the goal running k meters. 
Note that, fdf should punch card one by one which means that he will punch card at the first point before the second, even if he reaches the second point first. 
It is guaranteed that punching-card points and the point fdf starts running will appears randomly with equal probability and the three points won’t be coincide.

输入描述:

 
 
The first line contains an integer number T, the number of test cases. 
i th of each next T lines contains two integer numbers k,x(1 ≤ k,x ≤ 10 9).

输出描述:

For each test case print the probability of completing the goal, round to two decimal places.

思路:由于起跑点 ,打卡起点 S 和终点 T 三点不重合,因此可将 起跑点固定住作为开始点, 对 S ,T分析:

若 S 在 T前面 即 S<T :  跑步距离 Sx=T, 只要跑到 T点即可打卡完成

若 S 在 T 后面 即 S>T: Sx=x+T;    要先跑到 S点打卡在 跑一圈到 T点完成打卡

 Sx= S<T ? T : x+T  做出 S,T 的概率图 


则 Sx的概率 Px= S<T ? S绿 :S蓝

对于 k 要 Sx>=k 才能完成 计划 ,则对 K分析:

     Sx= S<T ? T : x+T

     Px= S<T ? S绿 :S蓝

  k<x :         p= (S绿&& Sx>=k)+S蓝 = S1/(x*x) + 0.5;

  k==x :       p=0.5;

  n<k<2*x:   p= S蓝&&Sx>=k = S2/(x*x);

  k>=2*x:     Sx<k --> p=0;

Code:

#include<iostream>
using namespace std;

long long T,n,k;

int main()
{
	ios::sync_with_stdio(false);
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&k,&n);
		double ans=0;
		if(k<n)	ans=(1-1.0*k*k/(n*n))/2+0.5;
		else	if(k==n)	ans=0.5;
		else	if(k<2*n)	ans=1.0*(2*n-k)*(2*n-k)/(2*n*n);
		printf("%.2lf\n",ans);
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值