HDOJ--2199Can you solve this equation?!!!!二分法

这是一篇关于使用二分法求解在0到100之间四次方程的ACM算法问题的文章。文章提供了方程的具体形式,并通过示例展示了如何应用二分法找到解。对于超出给定范围的方程解,输出'No solution!'。
原题链接:

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.
 

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
  
  
2 100 -4
 

Sample Output
  
  
1.6152 No solution!

题目大意:题目大意就是给你一个函数
Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100) ,然后给你F(X)的数值。计算在0-100区间内的解,如果解不在0-100区间内,那么输出No solution!

思路:二分,第一次尝试二分法,查了很多人的资料,初步理解了二分法。

代码:
#include<stdio.h>
#include<math.h>
double f(double x)
{
	return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6;
}
//函数求导可以发现,在0-100是单调递增的 
int main()
{
	double max=f(100);      //最大值 
	double min=f(0);		//最小值 
	int T;
	double Y,result,l,r;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lf",&Y);
		if(Y>max||Y<min)	//不在区间的判定 
		{
			printf("No solution!\n");
			continue;
		}
		else
		{
			l=0.0;r=100.0;
			while(r-l>1e-10)
			{
				result=f((l+r)/2);		//二分法 
				if(result<Y)
				{
					l=((l+r)/2);
				}
				else	r=((l+r)/2);
			}
			printf("%.4lf\n",(l+r)/2);
		}
	}
	return 0;
}
 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值