You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
694 B

5 years ago
#include <iostream>
using namespace std;
const double e = 2.718281828459;
double log(double x)
{
long t = 0;
while (x > 1)
{
x /= e;
++t;
}
x = x - 1;
double ret = 0;
int k = 1;
double temp;
double c = 1;
double xx = 1;
while (1)
{
xx *= x;
temp = c * xx / k;
c *= -1;
ret += temp;
++k;
if (temp > -0.00000001)
break;
}
return ret + t;
}
int main()
{
double a, x; // 计算log(a,x);
// 用ln(x)/ln(a)计算。
cin >> a >> x;
if (a <= 0 || x <= 0 || a == 1)
{
cout << "输入不正确!" << endl;
}
double ans = log(x) / log(a);
cout << ans << endl; // log(x)返回x的自然对数使用虚include"cmath"或"math.h"。
return 0;
}