在 C++ 中不使用 pow() 函数计算指数
C++ 与许多其他编程语言一样,C++ 带有一个内置库,其中包含函数和模块以简化任务。 pow()
函数是一个内置函数,可让我们轻松计算数字的幂。
但是,可能存在一种情况,例如在编码平台上,您必须在不使用任何内置函数的情况下找到数字的幂。 对你将如何做到这一点有什么猜测吗?
在本文中,我们将讨论在不使用 pow()
函数的情况下计算数字指数的各种方法。
在 C++ 中不使用 pow() 函数计算指数
在我们继续使用 pow() 函数的替代方法之前,让我们先看看 pow() 函数在 C++ 中是如何工作的。
C++ 中的 pow() 函数
顾名思义,pow()
或幂函数计算数字的幂。 此函数采用两个值作为参数,并且还需要使用头文件。
我们传入 pow()
函数的第一个值作为基数,第二个值是基数必须提高到的指数。 这是一个借助 pow()
函数计算 5^3 值的程序。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
double b=5.0, p=3.0, ans;
ans = pow(b,p);
cout << ans;
return 0;
}
输出结果:
125
请注意 ,
pow()
函数采用 double 值而不是 int 作为参数。 但这并不意味着pow()
函数不能用于整数。
但是,有时将 int 与 pow() 函数一起使用可能会在某些编译器中产生荒谬的输出。 例如,对于某些编译器,pow(4,2)
的输出可能为 15。
因此,建议使用具有双精度数据类型的 pow() 函数。
现在您已经了解了 pow() 函数的基础知识,让我们讨论一些在不使用 C++ 中的 pow() 函数的情况下计算数字指数的其他方法。
在 C++ 中使用 for 循环计算指数而不使用 pow() 函数
我们知道,在计算一个数的指数时,我们会使用重复的乘法。 重复此乘法的次数取决于指数或底数的幂。
这是一个相同的例子。
5^4 = 5 * 5 * 5 * 5 = 625
我们也可以在使用 for 循环的编程中应用这个概念。 这是相同的代码。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
int i, e, b, ans=1;
cout << "Enter the base: " << endl;
cin >> b;
cout << "Enter the exponent: " << endl;
cin >> e;
/*logic to calculate power*/
for(i= 1; i <= e; ++i)
{
ans = ans*b;
}
cout << "The solution is: " << ans;
return 0;
}
输出如下:
Enter the base:
5
Enter the exponent:
4
The solution is: 625
那么这段代码中发生了什么?
变量 b 和 e 分别指基数和指数。 此外,变量 ans 的值为 1,变量 i 定义了 for 循环运行的次数。
现在明白这里使用 pow() 函数的逻辑是从这段代码中的 for 块开始的。 因此,让我们分解一下 for 块中发生的事情。
你可以看到 i 被初始化为 1,它上升到指数的值,这里是 4。 此外,循环内的语句将 b 与 ans 相乘,并将结果存储在变量 ans 本身中。
这是变量 ans 在每次迭代中包含的内容。
i=1
ans = 1*5
ans = 5
i=2
ans = 5*5
ans = 25
i=3
ans = 25*5
ans = 125
i=4
ans = 125*5
ans = 625
显然,当循环终止时,变量 ans 的最终值作为输出返回。 这本质上就是如何在没有 pow() 函数的情况下使用 for 循环来计算数字的指数。
现在让我们使用 while 循环来执行相同的操作。
在 C++ 中使用 while 循环计算指数而不使用 pow() 函数
使用 while 循环计算数字指数的基本思想与 for 循环相同。 唯一改变的是计算重复次数的方法。
看看下面给出的代码。
在 while 块中,e 的初始值是用户输入的指数,每一步,该值减一。 最后,当 e 减少到 0 时,循环终止。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
int i, e, b, ans=1;
cout << "Enter the base: " << endl;
cin >> b;
cout << "Enter the exponent: " << endl;
cin >> e;
/*logic to calculate power*/
while(e!=0){
ans = ans * b;
--e;
}
cout << "The solution is: " << ans;
return 0;
}
输出结果:
Enter the base:
5
Enter the exponent:
4
The solution is: 625
您可以看到 while 循环内的语句将 b 与 ans 相乘,并将结果存储在变量 ans 本身中,其方式与 for 循环的情况相同。
这是变量 ans 在每次迭代中包含的内容。
e = 4
ans = 1 * 5
ans = 5
e = 3
ans = 5 * 5
ans = 25
e = 2
ans = 25 * 5
ans = 125
e = 1
ans = 125 * 5
ans = 625
因此,这就是您可以在不使用 pow() 函数的情况下使用 while 循环计算数字指数的方法。
如果您还想对输出执行任何其他操作,您可以按照下面的示例所示进行操作。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
int i, e, b, ans=1;
cout << "Enter the base: " << endl;
cin >> b;
cout << "Enter the exponent: " << endl;
cin >> e;
/*logic to calculate power*/
while(e!=0){
ans = ans * b;
--e;
}
/*adding 500 to the result*/
ans = ans + 500;
cout << "The solution after addition is: " << ans;
return 0;
}
输出结果如下:
Enter the base:
5
Enter the exponent:
4
The solution after addition is: 1125
请注意
,当基数和指数为整数时,建议使用这些方法计算指数。 对于浮点值,使用pow()
函数。
这两种方法的复杂度都是 O(n),n 是指数。
到目前为止,我们已经讨论了求一个数的幂的常用循环方法。 现在让我们讨论一个非常有趣的递归解决方案来做同样的事情。
在 C++ 中不使用 pow() 函数使用递归计算指数
看看下面给出的代码。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int product(int a, int b)
{
if(b)
return (a + product(a, b-1));
else
return 0;
}
int calculate_power(int x, int y)
{
if(y)
return product(x, calculate_power(x, y-1));
else
return 1;
}
int main()
{
cout << calculate_power(5, 2);
getchar();
return 0;
}
输出结果:
25
此代码使用两个函数,用于重复乘法的 product 和计算 x^y 的 calculate_power。
当主块运行时,控制首先转到 calculate_power 函数。 在此函数内部,如果指数 y 的值为 0,则控制转到 if 块,函数返回 1。
否则,控制转到 if 块,函数调用 product
函数,递归调用 calculate_power
函数。 此外,在产品功能内部,当控制转到 if 块时,产品功能将再次递归调用。
下面可以帮助您更好地理解这段代码的工作原理。
Coming down: calculate_pow(5,2)
↓
product(5, calculate_pow(5,1))
↓
product(5, calculate_pow(5, 0)
↓
return 1
Going back, Step 1: product(5, 1)
↓
return 5 + product(5, 0)
↓
return 0
↓
return 5+0 = 5
Going back, Step 2: product(5, 5)
↓
5 + product(5, 4)
↓
5 + 5 + product(5, 3)
↓
5 + 5 + 5 + product(5, 2)
↓
5 + 5 + 5 + 5 + product(5, 1)
↓
5 + 5 + 5 + 5 + 5
↓
25
这就是递归计算一个数的幂的方式,时间复杂度为 O(n) ,其中 n 是指数。
总结
在本文中,我们讨论了如何在不使用 C++ 中的 pow()
函数的情况下计算数字的指数。 我们使用 for 和 while 循环来执行此操作,还了解了一旦我们获得数字的幂后如何进一步执行其他计算。
最有趣的是,我们讨论了计算一个数的幂的递归解决方案,还通过详细的流程图了解了该代码的工作原理。
相关文章
Arduino 复位
发布时间:2024/03/13 浏览次数:315 分类:C++
-
可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。
Arduino 的字符转换为整型
发布时间:2024/03/13 浏览次数:181 分类:C++
-
可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。
Arduino 串口打印多个变量
发布时间:2024/03/13 浏览次数:381 分类:C++
-
可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。