
幂函数是使用乘法运算符进行计算的,即5n等于5*5*5… n次。为了使该函数在不使用乘法(*)和除法(/)运算符的情况下正常工作,我们将使用嵌套循环来重复添加数字n次。
#include <iostream>
using namespace std;
int main() {
int a= 4 , b = 2;
if (b == 0)
cout<<"The answer is"<<1;
int answer = a;
int increment = a;
int i, j;
for(i = 1; i < b; i++) {
for(j = 1; j < a; j++) {
answer += increment;
}
increment = answer;
}
cout<<"The answer is "<<answer;
return 0;
}The answer is 16
以上就是在C程序中,编写自己的幂运算函数,但不能使用乘法(*)和除法(/)操作符的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号