HDU 2199 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): 2091 Accepted Submission(s): 1053
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
2
100
-4
Sample Output
1.6152
No solution!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
//Constant Declaration
/*--------------------------*/
//#define LL long long
#define LL __int64
const int M=1000000;
const int INF=1<<30;
const double EPS = 1e-11;
const double PI = acos(-1.0);
/*--------------------------*/
// some essential funtion
/*----------------------------------*/
void Swap(int &a,int &b){ int t=a;a=b;b=t; }
int Max(int a,int b){ return a>b?a:b; }
int Min(int a,int b){ return a<b?a:b; }
int Gcd(int a,int b){ while(b){b ^= a ^=b ^= a %= b;} return a; }
/*----------------------------------*/
//for (i = 0; i < n; i++)
/*----------------------------------*/
#define F(x) (8*pow(x,4) + 7*pow(x,3) + 2*pow(x,2) + 3*x + 6 - y)
double y;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t, case1 = 0;
scanf("%d", &t);
int n, m;
int i, j;
//scanf("%d%d", &n, &m);
while (t--)
{
scanf("%lf", &y);
if (F(0.0) * F(100.0) > 0)
{
puts("No solution!");
}
else
{
double l = 0, r = 100;
if (F(l) == 0)
{
printf("%.4lf", 0);
goto end;
}
if (F(r) == 0)
{
printf("%.4lf", 100);
goto end;
}
double min;
while (r - l > EPS)
{
min = (l + r) / 2;
if (F(min) == 0)
{
goto end;
}
if (F(l) * F(min) < 0)
{
r = min;
}
else
{
l = min;//写成l = min + 1就错
}
}
end :
printf("%.4lf\n", min);
}
}
return 0;
}
posted on 2012-03-20 16:53 [S*I]SImMon_WCG______* 阅读(418) 评论(0) 编辑 收藏 举报