zoukankan      html  css  js  c++  java
  • HDU2199(二分搜索无限逼近)

    Can you solve this equation?

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


    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
     
    好吧这题真的有点跪了,做了那么久,用二分来无限逼近一个准确值,则将二分循环的结束条件设置为小于该精度,为了准确输出,自然要把该精度设置得更加细。
     

    #include <cstdio>
    #include <cmath>

    double result(double m)
    {
    double s=8*pow(m*1.0,4)+7*pow(m*1.0,3)+2*m*m+3*m+6;
    return s;
    }
    int main()
    {
    int t;double y;
    scanf("%d",&t);
      while (t--)
      {
       scanf("%lf",&y);
       double mid2;
       if(y<6||y>result(100)) puts("No solution!");
       else
       {
        double min2=1.0,max2=100.0;
        while ((max2-min2)>1e-6)
        {
          mid2=(max2-min2)/2+min2;
          double sum=result(mid2);
          if (sum==y) break;
          if (sum<y) min2=mid2;
          else max2=mid2;
         }
         printf("%.4f\n",mid2);
       }
      }
    return 0;
    }

  • 相关阅读:
    *****Exercise 4.1 Generate a multiplication table
    Exercise 3.4 A calculator that allows multiple calculations
    Exercise 3.3 Calculate a discounted price
    Exercise 3.2 Display a date
    理解Java异常处理机制
    eclipse远程Debug配置
    [转]java代码性能优化总结
    [转]A星寻路算法
    JVM参数配置
    Gradle笔记(1) 安装
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2873360.html
Copyright © 2011-2022 走看看