练习二1001

来源:互联网 发布:淘宝卡哪个好 编辑:程序博客网 时间:2024/06/08 04:21

Can you solve this equation?

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 220   Accepted Submission(s) : 83
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;<br>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<br>100<br>-4<br>
 


Sample Output
1.6152<br>No solution!<br>
 


Author
Redow
 


Statistic |Submit | Back
题意:
8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,给出y,求出满足条件的x。
思路:
单调函数,用二分法,注意终止条件要小一点,否则得不出结果。

代码:

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double zhi(double x)
{
 return (8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6);
}
int main()
{
 double t,y;
 cin>>t;
 for(int i=0;i<t;i++)
 {
  cin>>y;
  double low=0,high=100,mid;
  if(zhi(0)>y||zhi(100)<y)
   cout<<"No solution!"<<endl;
  else
  {
   while(high-low>0.00000001)
   {
    mid=(high+low)/2;
    if(zhi(mid)>y)
     high=mid;
    else low=mid;
   }
   cout<<setiosflags(ios::fixed)<<setprecision(4)<<mid<<endl;
  }
 }
 return 0;
}


0 0
原创粉丝点击