【求帮助 】用递归输出对数表

C++语言 码拜 8年前 (2016-01-29) 923次浏览
例如:以10为底,100和1000的对数分别为2,3。100和1000的几何平均值为316.23,其对数(2和3)的算术平均值为2.5.所以316.23的对数值即为2.5。这个过程可以继续下去:100和316.23的几何平均值为177.83。而177.83的对数则为(2+2.5)/2=2.25。
编写一个递归函数输出log(1),log(2),log(3)…………………..
k=2次根号下(n*m),有log k=(log n+log m)/2
解决方案:20分
修改一下:

#include <iostream>
#include <cmath>
using namespace std;
double defeps=1e-15;
double inf =  1/defeps;//暂时用这个表示无穷大.
double lg(double x);
double lg(double m,double n,double vm,double vn,double x,double eps =1e-15){
       double t = sqrt(m*n);
       double vt =(vm+vn)/2 ;
       if(fabs(t-x)<=eps)
            return vt;
       if(t < x)
            return lg(t,n,vt,vn,x,eps);
       return lg(m,t,vm,vt,x,eps);
}
double lg(double x){
    if(x<=0)return -inf;
    if(x==1)return 0;
    if(x==10)return 1;
    if(x>10)
        return 1+lg(x/10);
    if(x<1)return -1+lg(x*10);
    return lg(1,10,0,1,x,defeps);
}
int main()
{
    for(int i=0;i<=100;i++)
        cout <<lg((double)i/10)<<endl;
    return 0;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明【求帮助 】用递归输出对数表
喜欢 (0)
[1034331897@qq.com]
分享 (0)