HDU 2199 || HDU 2899 (二分查找+数学问题)

http://acm.hdu.edu.cn/showproblem.php?pid=2199

/*

    二分查找;

*/


#include <iostream>
#include <cmath>
using namespace std;

double fun(double x)
{
    return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6;
}
double l,m,r,y;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf",&y);
        if(fun(0)<=y && y<=fun(100))
        {
            l = 0,r = 100;
            while(r-l > 1e-6)
            {
                m = (l+r)/2;
                double ans = fun(m);
                if(ans > y)
                {
                    r = m - 1e-7;
                }
                else
                {
                    l = m + 1e-7;
                }
            }
            m = (l+r)/2.0;
            printf("%.4lf\n",m);
        }
        else
        {
            puts("No solution!");
        }
    }
}





http://acm.hdu.edu.cn/showproblem.php?pid=2899

/*

    函数为 F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x;

    那么它的导函数为 F'(x) = 42 * x^6+48*x^5+21*x^2+10*x-y;

    设计函数,求函数  G'(x) = 42 * x^6+48*x^5+21*x^2+10*x 的值,将其减去输入的y值;

    可以看出,函数G'(x)为单调递增函数;

    如果对于100,G'(x)  - y <= 0,那么对于任意数【0-100】,G'(x)都<=0,则F(100)为整个函数最小值;

    否则,我们进行二分查找;代码:

*/


#include <iostream>
#include <cmath>
using namespace std;

double l,m,r,y;
double g(double x)
{
	return 42*pow(x,6.0)+48*pow(x,5.0)+21*pow(x,2.0)+10*x;
}
double f(double x)
{
	return 6.0*pow(x,7.0)+8.0*pow(x,6.0)+7.0*pow(x,3.0)+5.0*pow(x,2.0)-y*x;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf",&y);
		if(g(100.0)-y>0)
		{
			l = 0,r = 100;
			while(r-l > 1e-8)
			{
				m = (l+r)/2;
				if(g(m) > y)
					r = m - 1e-7;
				else
					l = m + 1e-7;
			}
			m = (l+r)/2.0;
			printf("%.4lf\n",f(m));
		}
		else
			printf("%.4lf\n",f(100.0));
	}
}


你可能感兴趣的:(HDU 2199 || HDU 2899 (二分查找+数学问题))