1758 - 求指数方程的根(fxroot) [2*+] **

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

求指数方程的根(fxroot)
求方程f(x)=2^x+3^x-4^x=0 在[1,2]内的根。提示a^x可表示成exp(x*ln(a))或用math库中的power(a,x)。
输入:无输入
输出:f(x)=0的根,x精确到小数点10位
提示:记方程f(x)=0,若存在2个数x1和x2,且x1

题目输入

题目输出

输入/输出样例

输入格式


                        

输出格式


                        

C++解答

#include <iostream>
#include <cmath>
using namespace std;
int i,j,n;
double f(double x)
{
    return(pow(2,x)+pow(3,x)-pow(4,x));
}
main()
{
      double x,r,l;
      r=2;l=1;
      x=(r+l)/2;
      while (f(x)!=0 && r-l>0.000000001)
      {
            if (f(r)*f(x)<0) l=x;
            else r=x;
            x=(r+l)/2;
      }
      printf("%0.10lf",x);
}