【发布时间】:2018-03-27 17:58:14
【问题描述】:
我想在 c++ 中编写一个函数来计算一个数字对任何底的对数
这个函数应该能够计算任意底数的对数
#include <bits/stdc++.h>
using namespace std;
int main()
{
int number,base;
int i=0;//this is the counter
double value=0; //the value of the power
cout<<"enter the number : "<<endl;
cin>>number;
cout<<"enter the base : "<<endl;
cin>>base;
while (value<number){//if the value of the power <the number the loop will be continue
value=pow(base,i);
if (value==number) //this if statment to check if the result is correct or not
{
break;
}i+=1;
}cout<<"the result is : "<<i<<endl;//print the result on the screen
return 0;
}
【问题讨论】:
-
你的问题是?
-
你做了哪些研究?你知道如何在纸上进行计算吗?从那开始。
-
将
std::log的存在与您的数学(日志)知识相结合,以获得您想要的结果。提示:解决方案占用 1 行。 -
最后,欢迎来到 stackoverflow.com。请花一些时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请take the tour和read about how to ask good questions。
标签: c++ algorithm visual-c++ logarithm