zoukankan      html  css  js  c++  java
  • HDU 2199

    Can you solve this equation?

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 23060    Accepted Submission(s): 10003


    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!
     
    Author
    Redow
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2899 2289 2298 3400 1551 
     
    这是一道比较基础的纯粹二分算法的的题目。 这一题要求的就是8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,给定Y在0-100内找出一个数X是这个式子成立,当然也就是找出一个double数字使左边和右边最相近。我是按照二分,然后判断mid带入上面式子之后比10^-8小,但是经过实践是不行的,可以过案例但是会WA,我不是很明白可能是精度的问题,所以不要这样做。正确的做法是:从 low=0和high=100往中间挤,也就是当low和high的差很小时,mid的差别也很小,那么mid代入上式和Y的差距也就小了,实践证明开到e-8,e-7都是可以的。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #include <cmath>
     6 #define eps 1e-6 
     7 using namespace std;
     8 double A(double x)
     9 {
    10     return (8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * pow(x, 1) + 6);
    11 }
    12 int main()
    13 {
    14     int n;
    15     long long y, t;
    16     cin >> n;
    17     while (n--)
    18     {
    19         cin >> y;
    20         if (A(0)>y || A(100)<y)
    21         {
    22             cout << "No solution!" << endl;
    23             continue;
    24         }
    25         if (y == 0) cout << 0 << endl;
    26         else
    27         {
    28             double l = 0, r = 100;
    29             double mid;
    30             while (r - l >=eps)
    31             {
    32                 mid = (l + r) / 2;
    33                  if (A(mid) > y)
    34                 {
    35                     r = mid;
    36                 }
    37                 else l = mid;
    38             }
    39             printf("%.4lf
    ", mid);
    40         }
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    二分查找
    Uva11464 Even Parity
    Uva10881 Piotr's Ants
    POJ3154 Graveyard
    [NOIP2015] 提高组 洛谷P2680 运输计划
    [NOIP2015] 提高组 洛谷P2679 子串
    [NOIP2015] 提高组 洛谷P2678 跳石头
    [NOIP2015] 提高组 洛谷P2668 斗地主
    [NOIP2015] 提高组 洛谷P2661 信息传递
    [NOIP2015] 提高组 洛谷P2615 神奇的幻方
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271217.html
Copyright © 2011-2022 走看看