长安大学第三届ACM-ICPC程序设计竞赛(同步赛) B-Happy Running
链接:https://www.nowcoder.com/acm/contest/102/B
来源:牛客网
题目描述
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.
ith 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.
个人抠脚翻译:
题目描述
“悦跑圈”,一款跑步应用,在CHD非常出名。
为了减肥,fdf决定每天带着“悦跑圈”跑k米。我们把学校看作一个总长度为x米的圆形跑道,并且fdf可以在跑道上的一个随机点 顺时针运行。“悦跑圈”将随机显示两个打卡点。由于体力不佳,fdf决定仅通过完成打卡来完成跑步,无论是否完成k米的目标。
一天在跑步之前,fdf想知道他是否能够完成今天的目标,并请求你帮助他计算完成k米的目标的概率。
请注意,fdf应该一张张地打卡,这意味着即使他第一次到达第二点,他也会在第二点之前的第一点打卡。
保证冲卡点和fdf开始运行的点将以相同的概率随机出现,三点不会重合。
输入描述
第一行包含一个整数T,即测试用例的数量。
接下来下T行中的第 i 行包含两个整数k,x(1 ≤ k,x ≤ 10^9)。
输出描述
对于每个测试案例,打印完成目标的概率,保留小数点后两位。
示例1
输入
3
2 2
4 3
2 1
输出
0.50
0.22
0.00
思路
数学题。
分情况讨论。
- 情况1:k大于两圈;
- 情况2:k小于一圈;
- 情况3:k在一圈到两圈之间。
AC代码
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int T;
cin >> T;
while(T--) {
double k,x;
cin >> k >> x;
double ans;
if (k>=2*x) {
ans = 0;
} else if (k<x) {
double in = k/x;
double out = 1-in;
ans = in*in/2+in*out+out*in+out*out;
} else {
double in = (k-x)/x;
double out = 1-in;
ans = out*out/2;
}
cout << fixed << setprecision(2) << ans << endl;
}
return 0;
}