本文实例演示了C语言求幂计算的高效解法。很有实用价值。分享给大家供大家参考。具体方法如下:

题目如下:

给定base,求base的幂exp

只考虑基本功能,不做任何边界条件的判定,可以得到如下代码:

#include

using namespace std;

int cacExp(int base, int exp)

{

int result = 1;

int theBase = 1;

while (exp)

{

if (exp & 0x01)

result = result * base;

base = base * base;

exp = exp >> 1;

}

return result;

}

int getRecurExp(int base, int exp)

{

if (exp == 0)

{

return 1;

}

if (exp == 1)

{

return base;

}

int result = getRecurExp(base, exp >> 1);

result *= result;

if (exp & 0x01)

result *= base;

return result;

}

int main()

{

for (int i = 1; i < 10; i++)

{

int result = cacExp(2, i);

//int result = getRecurExp(2, i);

cout << "result: " << result << endl;

}

return 0;

}

再来看看数值的整数次方求解方法:

#include

using namespace std;

bool equalZero(double number)

{

if (number < 0.000001 && number > -0.000001)

return true;

else

return false;

}

double _myPow(double base, int exp)

{

if (exp == 0)

return 1;

if (exp == 1)

return base;

double result = _myPow(base, exp >> 1);

result *= result;

if (exp & 0x01)

result *= base;

return result;

}

double _myPow2(double base, int exp)

{

if (exp == 0)

return 1;

double result = 1;

while (exp)

{

if (exp & 0x01)

result *= base;

base *= base;

exp = exp >> 1;

}

return result;

}

double myPow(double base, int exp)

{

if (equalZero(base))

return 0;

if (exp == 0)

return 1;

bool flag = false;

if (exp < 0)

{

flag = true;

exp = -exp;

}

double result = _myPow2(base, exp);

if (flag)

{

result = 1 / result;

}

return result;

}

void main()

{

double base = 2.0;

int exp = -5;

double result = myPow(base, exp);

cout << "result: " << result << endl;

}

相信本文所述对大家C程序算法设计的学习有一定的借鉴价值。

c语言乘方程序,C语言求幂计算的高效解法相关推荐

  1. c语言求幂 编程,C语言求幂计算的高效解法

    本文实例演示了C语言求幂计算的高效解法.很有实用价值.分享给大家供大家参考.具体方法如下: 题目如下: 给定base,求base的幂exp 只考虑基本功能,不做任何边界条件的判定,可以得到如下代码: ...

  2. c语言乘方程序,c语言乘方(c语言乘方表示)

    C语言中没有乘方运算符,但有计算乘方的函数:pow 函数原型如下:#include //引用头文件 double pow(double x, double y) //函数定义方法 表示求x的y次方.例 ...

  3. C语言编写程序,分别求100以内的奇数之和、偶数之和。(用for语句实现)

    C语言编写程序,分别求100以内的奇数之和.偶数之和.(用for语句实现) 运行代码: #include <stdio.h> int main() {int i,odd=0,even=0; ...

  4. 常用c语言小程序,c语言经典小程序汇总大全

    网上有很多的人说编程有多么多么无聊,其实:不要管别人怎么说,别人说什么,做你自己喜欢做的事就好.坚持下来,你会发现编程的乐趣的.当然,如果你觉得学习编程语言很痛苦,坚持了一段时间后无果,南无果断放弃未 ...

  5. c语言小程序作业,c语言小程序(c语言简单小程序代码)

    所以特此求经典C语言小程序.谢谢大家的关注!!! #include #include void function(int n){ int i,j,k,x=0; for(i=1;i<=n;i++) ...

  6. c语言名字程序,c语言获得程序位数和操作系统位数和名称

    // vcis64.cpp : VC 64位程序开发心的--c语言获得程序位数和操作系统位数和名称. #include #include #include #include // 获取程序位数(被编译 ...

  7. c语言实验程序,C语言实验程序

    <C语言实验程序>由会员分享,可在线阅读,更多相关<C语言实验程序(11页珍藏版)>请在人人文库网上搜索. 1.P113:6 #include void main() floa ...

  8. C语言经典程序之:求十个整数中的最大最小数并输出(指针)

    #include "stdio.h" void max(int *p,int N) /*求最大数的函数(void--无返回值),定义指针*/ {int i,max=*p; /*定义 ...

  9. c语言编写程序平均值,编写求一组整数的和与平均值的程序

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 ----------------------------------------------------- [习题6-1]编写求一组整数的和与平均值的程序 ...

  10. C语言编程序编数独,求用C语言编一个解九宫格数独的程序怎么办? 爱问知识人...

    "前两天刚写完,还没优化,已运行通过了. 晕,一维的好麻烦,这个也是碰巧前两天刚写好的,你看着自己修改下 #include typedef struct { int line; int ro ...

最新文章

  1. 利用Mininet进行VxLAN验证实验
  2. PAT L2-005 集合相似度
  3. SharePoint文档上传管理
  4. 【Filebeat】logstash 和filebeat 是什么关系
  5. sql里 where和order by一起使用是怎样的顺序
  6. 【SQL】MERGE
  7. Android常用抓包工具—Charls(青花瓷)
  8. eclipse tomcat 热部署
  9. Beyond Compare 4 “授权秘钥已被吊销“ 的解决办法
  10. class SequenceFileOutputFormat takes type parameters
  11. 狂神说Java--Java学习笔记(基础合集)
  12. 微信小程序“奶茶屋”的设计与实现
  13. 数据分析基础——数据规整
  14. 电脑重装系统经验总结
  15. 带你理解交换机基本原理和配置
  16. 点到点的距离、点到直线的距离、点是否在直线上
  17. 2021.4.22 易协同访客数据
  18. 文件exer1的访问权限为rw-r--r--,现要增加所有用户的执行权限和同组用户的写权限,下列哪个命令是对的?
  19. 婚车租赁APP开发源码部署
  20. BaiduPCS-Go 使用CMD命令行全速下载百度云

热门文章

  1. 前端三件套之CSS(二)
  2. An error occurred while starting the application
  3. 怎么把日程提醒放在手机桌面
  4. 老罗的工匠精神是不是有唯一性
  5. HLS第十二课(bayer photo)
  6. 云端编译android,Android原生插件开发云端打包问题
  7. (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  8. 【Java】面向对象(二)继承
  9. excel中roundup使用指南
  10. 【玩转linux】head命令