//单调函数
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;<br>Now please try your lucky.
#include<bits/stdc++.h>
using namespace std;
double y;//y定义成全局变量
double f(double x)//注意要定义成double 类型函数
{
return 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6-y;
}
int main()
{
int t,i,j;
cin>>t;
while(t--)//t组数据
{
cin>>y;
if(f(0)>=0||f(100)<=0){cout<<"No solution!"<<endl;}//两种特殊情况
else
{
double a=0,b=100,mid;
mid=(a+b)/2;
while(abs(b-a)>0.0000001)//二分法精度要求
{
if(f(mid)>0)
b=mid;
else
a=mid;
mid=(a+b)/2;//进行二分
}
printf("%.4lf\n",mid);输出
}
}
}
using namespace std;
int y;
double f1(double x)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-y*x;
}
double f2(double x)
{
return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;//求其极值点
}
int main()
{
int t,i,j;
double a,b,mid;
cin>>t;
while(t--)
{
cin>>y;
if(f2(0)>=0){cout<<f1(0)<<endl;}
else if(f2(100)<=0)cout<<f1(100)<<endl;
else
{
a=0;b=100;mid=(a+b)/2;
while(abs(b-a)>0.0000001)
{
if(f2(mid)>0)//找出极值点大体位置
b=mid;
else
a=mid;
mid=(a+b)/2;
}
printf("%.4lf\n",f1(mid));//输出
return 0;
}
}
}