程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 搜索練習 Can you solve this equation?,hduequation

HDU 搜索練習 Can you solve this equation?,hduequation

編輯:C++入門知識

HDU 搜索練習 Can you solve this equation?,hduequation


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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved