题意
有非负整数d,如果d=a+b,d=a*b,输出Y a b,(a,b是非负浮点型,保留九位有效数字),如果不能分,输出N。
sample input
7 69 0 1 4 5 999 1000
sample output
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
题解:
二分水题,二分求x*x-d*x+d=0这个函数的非负零点,找到的点即为a的值。注意精度问题。
渣代码一览:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
const double EPS=1e-10;
double binarysearch(int n){
double low=0.0,high=n,mid;
while(low<=high){
mid=(low+high)/2.0;
if(fabs(0.0-(mid*mid-n*mid+n))<=EPS)
break;
else if(mid*mid-n*mid+n>EPS){
high=mid;
}
else if(0.0-(mid*mid-n*mid+n)>EPS) {
low=mid;
}
}
return mid;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
if(n>0&&n<4){
cout<<"N"<<endl;
}
else if(n==0){
cout<<"Y 0.000000000 0.000000000"<<endl;
}
else if(n==4){
cout<<"Y 2.000000000 2.000000000"<<endl;
}
else{
double temp=binarysearch(n);
cout<<"Y "<<setprecision(9)<<fixed<<temp<<" "<<n-temp<<endl;
}
}
return 0;
}