• UVa 10341 (二分求根) Solve It


    很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可。

     1 #include <cstdio>
     2 #include <cmath>
     3 //using namespace std;
     4 
     5 const double e = 1e-14;
     6 double p, q, r, s, t, u;
     7 
     8 inline double f(double x)
     9 { return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u; }
    10 
    11 int main()
    12 {
    13     //freopen("in.txt", "r", stdin);
    14 
    15     while(scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u) == 6)
    16     {
    17         if(f(0)<-e || f(1)>e) { puts("No solution"); continue; }
    18         double L = 0, R = 1, m;
    19         for(int i = 0; i < 30; i++)
    20         {
    21             m = (L+R)/2;
    22             if(f(m) < 0) R = m;
    23             else L = m;
    24         }
    25         printf("%.4f
    ", m);
    26     }
    27 
    28     return 0;
    29 }
    代码君
  • 相关阅读:
    162. Find Peak Element
    475. Heaters
    字符串统计
    数据的交换输出
    偶数求和
    青年歌手大奖赛_评委会打分
    蟠桃记
    素数判定
    多项式求和
    出现Presentation Error的解决方法
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4338394.html
Copyright © 2020-2023  润新知