NYOJ 题目969

解方程(二)

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
  Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
输入
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
输出
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
样例输入
2
100
200
样例输出
-74.4291
-178.8534
一开始看到题目也是比较没有头绪,后来经人提醒知道了方法,求出导数,因为其导数的正负可以决定方程的单调性而导数的最大最小值正负其实很大一部分是由Y决定的,所以先判断导数最大最小值,最大值小于零,说明最小值为F(100),最小值大于零,说明F(0)为最小值,有正有负需要寻找零点,零点的寻找利用二分查找法。
#include<stdio.h>
#include<math.h>
double fun1(double n,double y)
{
    return 6*pow(n,7)+8*pow(n,6)+7*pow(n,3)+5*pow(n,2)-y*n;
}
double fun2(double n,double y)
{
    return 42*pow(n,6)+48*pow(n,5)+21*pow(n,2)+10*n-y;
}
int main()
{
    double n,i,y;
    double fun1(double n,double y);
    double fun2(double n,double y);
    scanf("%lf",&n);
    while(n--)
    {
        scanf("%lf",&y);
        double number1=42*pow(100,6)+48*pow(100,5)+21*pow(100,2)+10*100;
        if(y>number1)
        {
            printf("%.4lf\n",fun1(100,y));
            continue;
        }
        if(y<=0)
        {
            printf("%.4lf\n",fun1(0,y));
            continue;
        }
        double a=0,b=100,c=fun2(0,y),d=fun2(100,y),e;
        while((b-a)>1e-10)
        {
            e=(a+b)/2;
            if(fabs(fun2(e,y))<1e-10)
                break;
            else
                if(fun2(e,y)*c<0)
                {
                    d=fun2(e,y);
                    b=e;
                }
                else
                {
                    c=fun2(e,y);
                    a=e;
                }
        }
        printf("%.4lf\n",fun1(e,y));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值