码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 搜索练习 Can you solve this equation?

时间:2016-04-13 15:56:42      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

Can you solve this equation?

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

 

Sample Output
1.6152
No solution!

 

简单题意:

  给出一个方程,求解方程,注意Y的范围。

思路:

  用二分法求解,

# include <iostream>
# include <cmath>
using namespace std;
double f(double x, double y)
{
    double fx = 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6 - y;
    return fx;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        double y;
        cin >> y;
        double begin = 0, end = 100, mid;
        int i = 0;
        while(1)
        {
            if(y < 6 || y > 8.0702e+8)
            {
                cout << "No solution!" << endl;
                break;
            }

            mid = (begin + end) / 2;
            if(fabs(f(mid, y)) <= 0.0001)
            {
                cout.precision(4);
                cout << fixed <<  mid << endl;
                break;
            }
            else if(f(mid, y) > 0)
            {
                end = mid;
            }
            else if(f(mid, y) < 0)
            {
                begin = mid;
            }
            //cout << mid << endl;
        }

    }


    return 0;
}

 

HDU 搜索练习 Can you solve this equation?

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5387393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!