hdu Can you solve this equation

简介:

Can you solve this equation?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 164 Accepted Submission(s): 94
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!
这道题用的是二分搜索法

二分法原理

 给定方程f(x)=0,f(x)在区间[a,b]连续,且f(a)f(b)<0,则方程f(x)(a,b)

至少有一根,为便于讨论,不妨设方程f(x)=0(a,b)内只有一实根    

采取使有根区间逐步缩小,从而得到满足精度要求的实根     

的近似值。   取[a,b]区间二等分的中点x0 =(a+b)/2,

f(x0)=0,x0f(x)=0的实根  f(a)f(b)<0 成立,

则必在区间(a, x0),a1=a,b1= x0;

否则必在区间(x0,b),a1= x0,b1=b,

这样,得到新区间(a1,b1),其长度为[a,b]的一半,

如此继续下去,进行k次等分后,得到一组不断缩小的区间,

[a,b],[a1,b1],......[ak,bk].

 

分析本题得:函数是一个单调递增的函数;所以此函数的0点只有一个。本题的精度为0.0001,保证小数点后前四位相同。

复制代码
 1 #include <iostream>
2 #include <stdio.h>
3 #include <math.h>
4 using namespace std;
5 double y;
6 double func(double x)
7 {
8 return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
9 }
10 int main()
11 {
12 int t;
13 double left,right,mid,ans1,ans2,ans3;
14 cin>>t;
15 while(t--)
16 {
17 cin>>y;
18 if(func(0)>y||func(100)<y)
19 cout<<"No solution!"<<endl;
20 else
21 {
22 left = 0.0;
23 right = 100.0;
24 mid = 50.0;
25 ans1 = func(left)-y;
26 ans2 = func(mid)-y;
27 ans3 = func(right)-y;
28 while(fabs(ans1-ans2)>0.0001)
29 {
30 if(ans2>0)
31 {
32 right = mid;
33 ans3 = ans2;
34 mid = (left+right)/2;
35 }else
36 {
37 left = mid;
38 ans1 = ans2;
39 mid = (left+right);
40 }
41 ans2 = func(mid)-y;
42 }
43 printf("%.4f\n",mid);
44 }
45 }
46 return 0;
47 }
复制代码
本文转自NewPanderKing51CTO博客,原文链接: http://www.cnblogs.com/newpanderking/archive/2011/08/24/2152439.html  ,如需转载请自行联系原作者
相关文章
codeforces 285C - Building Permutation
题目大意是有一个含n个数的数组,你可以通过+1或者-1的操作使得其中的数是1--n中的数,且没有重复的数。 既然是这样的题意,那么我就应该把原数组中的数尽量往他最接近1--n中的位置放,然后求差绝对值之和,但有多个数,怎么使他们和最小,这样就要对其进行排序了,直接按大小给它们安排好位置,然后计算。
32 0
|
6月前
|
Java
HDU-2199-Can you solve this equation?
HDU-2199-Can you solve this equation?
36 0
|
6月前
|
Java
HDU-2199-Can you solve this equation
HDU-2199-Can you solve this equation
30 0
|
12月前
|
图形学
hdu1086 You can Solve a Geometry Problem too(判断线段相交)
hdu1086 You can Solve a Geometry Problem too(判断线段相交)
70 0
HDU-1097,A hard puzzle(快速幂)
HDU-1097,A hard puzzle(快速幂)
|
算法 Go
HDU-1548,A strange lift(Dijkstra)
HDU-1548,A strange lift(Dijkstra)
HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)
HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)
123 0
HDOJ(HDU) 2136 Largest prime factor(素数筛选)
HDOJ(HDU) 2136 Largest prime factor(素数筛选)
108 0
|
Go
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
106 0