sdau-2 1001

来源:互联网 发布:英语提高的方法 知乎 编辑:程序博客网 时间:2024/05/17 04:54

是时候写一波博客了!

问题描述:8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,测试T组,输入个Y,在0到100间找出解。找到就输出(保留四位小数),找不到就打印No solution!

解题思路:额,就一普通的二分法问题。直接套模板,真心没啥可说的。

做这题要细心,我一哥们做这题总WA,死活找不出错,最后才发现“No solution!”少了“!”。。。。。。。。。。。。。。。。。。。。。。一个悲伤的故事

代码:#include<iostream>
#include<cmath>
#include<algorithm>
#include<stdio.h>
using namespace std;
double judge(double m){
    double x;
    x=8*pow(m,4)+7*pow(m,3)+2*pow(m,2)+3*m+6;
    return x;
}
int main(){
    int T;
    cin>>T;
    while(T--){
        double low=0,high=100,mid=0;
        double Y;
        cin>>Y;
        if(judge(low)>Y||judge(high)<Y){
            cout<<"No solution!"<<endl;
            continue;
        }
        while(high-low>=1e-7){
            mid=(high+low)/2;
            if(judge(mid)==Y){
                break;
                high=(high+low)/2;
            }else if(judge(mid)>Y)
                high=mid;
             else
                 low=(high+low)/2;
        }
        printf("%0.4lf\n",(high+low)/2);
    }
    return 0; 
}

0 0