题目:
C. Meme Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Try guessing the statement from this picture:
You are given a non-negative integer dd. You have to find two non-negative real numbers aa and bbsuch that a+b=da+b=d and a⋅b=da⋅b=d.
Input
The first line contains tt (1≤t≤1031≤t≤103) — the number of test cases.
Each test case contains one integer dd (0≤d≤103)(0≤d≤103).
Output
For each test print one line.
If there is an answer for the ii-th test, print “Y”, and then the numbers aa and bb.
If there is no answer for the ii-th test, print “N”.
Your answer will be considered correct if |(a+b)−a⋅b|≤10−6|(a+b)−a⋅b|≤10−6 and |(a+b)−d|≤10−6|(a+b)−d|≤10−6.
Example
input
Copy
7
69
0
1
4
5
999
1000
output
Copy
Y 67.985071301 1.014928699
Y 0.000000000 0.000000000
N
Y 2.000000000 2.000000000
Y 3.618033989 1.381966011
Y 997.998996990 1.001003010
Y 998.998997995 1.001002005
题目:
#include<iostream>
#include<cstdio>
using namespace std;
#define NIL 1e-11
int main()
{
int t;
double d,l,r,mid;
cin >> t;
while(t--){
cin >> d;
l = d / 2;
r = d;
while(r - l > NIL){
mid = (l + r) / 2.0;
if((d - mid) * mid > d) l = mid;
else r = mid;
}
if(d * d - 4 * d < 0) cout << "N" << endl;//求根公式 <0误解
else printf("Y %.9lf %.9lf\n",r,d - r);//从大到小输出
}
return 0;
}
题意:
告诉你d,已知a +b = d,ab = d,让你求a,b如果无解输出N,如果有解输出Y,然后输出a和b,注意这里先要输出较大的那个,而且精度要保留到9位。
思路:
由a + b = d,a b = d,可以推导得出b^2 - b * d +d = 0,由求根公式可以得到方程是否有解,这里可以通过一般式直接解出a,b。但是最近一直在练习二分,所以我是通过二分去完成的,因为先要输出两解中较大的那个,而且a + b = d,加大的解的范围一定在[d / 2,d]之间,所以我们可以得到上下界,注意这里循环条件r - l一定大于足够小的数,因为这里精度要到9位。当(d - mid) * mid > d说明mid太小,要选后半部分,反之选前半部分。