首页 > 代码库 > 杭电2199.Can you solve this equation?
杭电2199.Can you solve this equation?
Problem Description
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;
Now please try your lucky.
Now please try your lucky.
Input
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 a real number Y (fabs(Y) <= 1e10);
Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input
2100-4
Sample Output
1.6152
No solution!
本题涉及二分法
#include<stdio.h>
#include<math.h>
double ans(double a)//计算(8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6)
{
return (8*a*a*a*a+7*a*a*a+2*a*a+3*a+6.0);
}
double ans(double);//函数的声明
int main()
{
double l,r,y,mid;
int n,flag;
scanf("%d",&n);
while(n--)
{
scanf("%lf",&y);
#include<math.h>
double ans(double a)//计算(8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6)
{
return (8*a*a*a*a+7*a*a*a+2*a*a+3*a+6.0);
}
double ans(double);//函数的声明
int main()
{
double l,r,y,mid;
int n,flag;
scanf("%d",&n);
while(n--)
{
scanf("%lf",&y);
//l为左端,r为右端;
l=0;
flag=0;
r=100;
l=0;
flag=0;
r=100;
//以下为二分法的精髓
while(l<r)
{
mid=(l+r)/2.0;
if(fabs(ans(mid)-y)<=1e-6)
{
printf("%.4lf\n",mid);
flag=1;
break;
}
else if(ans(mid)-y>1e-6)
{
r=mid;
}
else if(y-ans(mid)>1e-6)
{
l=mid;
}
while(l<r)
{
mid=(l+r)/2.0;
if(fabs(ans(mid)-y)<=1e-6)
{
printf("%.4lf\n",mid);
flag=1;
break;
}
else if(ans(mid)-y>1e-6)
{
r=mid;
}
else if(y-ans(mid)>1e-6)
{
l=mid;
}
}
if(!flag)
printf("No solution!\n");
}
return 0;
}
杭电2199.Can you solve this equation?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。