codeforces 1076C Meme Problem

题意

有非负整数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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值