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 bb such 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.
题目链接:http://codeforces.com/problemset/problem/1076/C
题意就不说了,一张图片说明了一切!
解题思路:a+b=d a⋅b=d, 这两个式子联立db−b^2−d=0, 然后根据数学公式算出二次函数在什么情况下是否有解,算出Δ,也就是数学中的b^2-4ac,找出解的范围,容易得出当d等于0时a=b=0, 在0-4之间是没有解的,要是有解的话,直接算出就可以了
#include <bits/stdc++.h>
using namespace std ;
typedef long double ld ;
const int Maxn = 1000 + 10 ;
int main (){
ios_base::sync_with_stdio(false) ;
cin.tie(0) ;
cout.tie(0) ;
cout << fixed << setprecision(9) ;
ld T;
ld d ;
cin >> T ;
while (T--){
cin >> d ;
if (d == 0) {
cout << "Y " << 0.0 << " " << 0.0 << endl ;
continue ;
}
if (d < 4) {
cout << "N" << endl ;
continue ;
}
ld D = sqrt(d * ld (d - 4)) ;
ld a = (d + D) / 2.0 ;
ld b = (d - D) / 2.0 ;
cout << "Y " << a << " " << b << endl ;
}
return 0 ;
}