hdu 2199 Can you solve this equation?

本文介绍了一种在一维区间内寻找方程根的数值计算方法。通过输入方程的目标值,采用二分法逐步缩小可能包含根的区间范围,最终达到较高的精度找到满足条件的根。该算法适用于形如8x^4 + 7x^3 + 2x^2 + 3x + 6 = y类型的方程求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;

double y;
double fcn(double x)
{
    double t=8*x*x*x*x+7*x*x*x+2*x*x+3*x+6-y;
    return t;
}

int main()
{
    int t;
    double x1,x2,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf",&y);
        x1=0,x2=100;

        if(fcn(x1)>0&&fcn(x2)>0)
        {
            printf("No solution!\n");
            continue;
        }
        if(fcn(x1)<0&&fcn(x2)<0)
        {
            printf("No solution!\n");
            continue;
        }

        if(fcn(x1)==0)
        {
            printf("%.4f\n",x1);
            continue;
        }
        if(fcn(x2)==0)
        {
            printf("%.4f\n",x2);
            //printf("%.2lf\n",fcn(x2));
            continue;
        }
        int f=0;
        while(x2-x1>1e-10)
        {
            x=(x1+x2)/2;
            double t1,t2,t3;
            t1=fcn(x1);
            t2=fcn(x);
            t3=fcn(x2);
            if(fcn(x1)==0)
            {
                f=1;
                break;
            }
            if(fcn(x2)==0)
            {
                f=2;
                break;
            }
            if(t1>0&&t2<0) x2=x;
            else if(t1<0&&t2>0) x2=x;
            else if(t2>0&&t3<0) x1=x;
            else if(t2<0&&t3>0) x1=x;
            //printf("%.4f\n",fcn(x1));
        }
        if(f==1) printf("%.4f\n",x1);
        else printf("%.4f\n",x2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值