1. 程式人生 > >uva-10341-二分法

uva-10341-二分法

題意:已知方程的根在0-1範圍內,求解方程的根,如果方程不存在根,那就輸出 no solution.

直接二分,保留四位小數.

 

#include "pch.h"
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include
<math.h> #include<iomanip> namespace cc { using std::cout; using std::endl; using std::cin; using std::map; using std::vector; using std::string; using std::sort; using std::priority_queue; using std::greater; using std::vector;
using std::swap; using std::stack; constexpr double MD = 1e-6; double p, q, r, s, t, u; double cal(double x) { return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x*x + u; } void solve() { while
(cin >> p >> q >> r >> s >> t >> u) { double l = 0, r = 1; if (cal(l)*cal(r) > 0) { cout << "No solution" << endl; continue; } while (r - l > MD) { double m = (l + r) / 2; if (cal(m)*cal(l) > 0) l = m; else r = m; } cout << std::setiosflags(std::ios::fixed) << std::setprecision(4) << l << endl;; } } }; int main() { #ifndef ONLINE_JUDGE freopen("d://1.text", "r", stdin); #endif // !ONLINE_JUDGE cc::solve(); return 0; }