1076C Meme Problem

版权声明:大家一起学习,欢迎转载,转载请注明出处。若有问题,欢迎纠正! https://blog.csdn.net/memory_qianxiao/article/details/84025673

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

题意:输入t组数据,每次输入d,求解有没有a+b=d,a*b=d,有就输出Y,a,b,没有就输出N。

题解:求根公式 由体面可以给出条件变形,a+b=d,a*b=d,------》b=d/a--->a+d/a=d ---> a^2-ad+d=0.

形如ax^2+bx+c=0,所以利用求根公式求解两根,-b+sqrt(b^2-4ac)/2,这里的a=1,b=-d,c=d,带入求解就是答案。

求解之前应该判断有没有解,b^2-4ac<0无解。

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,d;
    cin>>t;
    while(t--)
    {
        cin>>d;
        if(d*d-4*d<0)
            puts("N");
         else printf("Y %.9f %.9f\n",(d+sqrt(d*d-4*d))/2,(d-sqrt(d*d-4*d))/2);
    }
    return 0;
}

python:

t=int(input())
for i in range(t):
    d=float(input())
    if(d**2-4*d<0):print("N")
    else:
        print("Y",(d+(d**2-4*d)**0.5)/2,(d-(d**2-4*d)**0.5)/2)

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/84025673
今日推荐