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\n", m);
26     }
27
28     return 0;
29 }

代码君

时间: 2024-10-05 04:19:15

UVa 10341 (二分求根) Solve It的相关文章

UVA 10341 (二分查找+精度)

题意: 给你一个关于x的方程,给出变量的值,求出x: Problem F Solve It Input:standard input Output:standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x+ q*sin(x) + r*cos(x) +s*tan(x) +t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of mult

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

方程求根——二分法

二分法求根主要应用了区间套定理,这一算法实现简单且结果也迭代的较好,但对于复杂函数其结果不理想 1.代码 %%二分法求根 %%f为函数表达式,interval0为初始区间,epsilon为控制精度 function RD = Roots_dichotomy(f,interval0,epsilon) x_low = interval0(1);x_up = interval0(2);x_ave = (x_low+x_up)/2; %%作图 t = x_low:(x_up-x_low)/1000:x_

HDU - 1588 Gauss Fibonacci (矩阵快速幂+二分求等比数列和)

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob

hdu 3641 数论 二分求符合条件的最小值数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*==================================================== 二分查找符合条件的最小值 ======================================================*/ ll solve() { __int64 low = 0, high = INF, mid ; while(low <=

方程式求根

1.solve() 求根 syms x;              %定义x为symbol,即所求的根为x y=x*sin(x)-x; solve(y,x);          %求 y=xsinx-x的根x %   x-2y=5 和 x+y=6 syms x y; eq1=x-2*y-5; eq2=x+y-6; A=solve(eq1,eq2,x,y);    %求二元一次方程的根x y 2.diff() 求函数的微分 syms x y=4*x.^5; yprime=diff(y);    %

二分求幂,快速求解a的b次幂

一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) { int a, b; int ans = 1; cin >> a >> b; for (int i = 1; i <= b; i++) { ans *= a; } cout << ans; } 那么如何快速的求得a的b次幂呢?上面的代码还可以优化吗? 当然是ok的!下面就介绍一种方法-二分求幂. 二分求幂 所谓二分求幂,即是将b次幂用二进制表示,当二进制位k位

[LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

二分求幂(快速求幂,二进制求幂)

二分求幂, 非递归求法(二进制求法): 比如 2^5就是5个2相乘,按照5的二进制求 3^10就是8个3相乘,再2个3相乘. 处理幂的二进制,具体实现代码如下: long long quickmulti(long long a,long long b) { long long res=1; while(b) { if(b&1) //如果最后一位为1,则res*=a; res*=a; a*=a; //a*=a b>>=1; //b%=2 } return res; }