博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数值的整数次幂
阅读量:4212 次
发布时间:2019-05-26

本文共 551 字,大约阅读时间需要 1 分钟。

题目描述

给定一个 double 类型的浮点数 base 和 int 类型的整数 exponent,求 base 的 exponent 次方。

解题思路

下面的讨论中 x 代表 base,n 代表 exponent。

 

因为 (x*x)n/2 可以通过递归求解,并且每次递归 n 都减小一半,因此整个算法的时间复杂度为 O(logN)。

 

 

public double Power(double base, int exponent) {    if (exponent == 0)        return 1;    if (exponent == 1)        return base;    boolean isNegative = false;    if (exponent < 0) {        exponent = -exponent;        isNegative = true;    }    double pow = Power(base * base, exponent / 2);    if (exponent % 2 != 0)        pow = pow * base;    return isNegative ? 1 / pow : pow;}

 

转载地址:http://ndkmi.baihongyu.com/

你可能感兴趣的文章
JAVA实现文件树
查看>>
linux -8 Linux磁盘与文件系统的管理
查看>>
linux 9 -文件系统的压缩与打包 -dump
查看>>
PHP在变量前面加&是什么意思?
查看>>
ebay api - GetUserDisputes 函数
查看>>
ebay api GetMyMessages 函数
查看>>
php加速器 - zendopcache
查看>>
手动12 - 安装php加速器 Zend OPcache
查看>>
set theme -yii2
查看>>
yii2 - 模块(modules)的view 映射到theme里面
查看>>
yii2 - controller
查看>>
yii2 - 增加actions
查看>>
php图像处理函数大全(缩放、剪裁、缩放、翻转、旋转、透明、锐化的实例总结)
查看>>
magento url中 uenc 一坨编码 base64
查看>>
强大的jQuery焦点图无缝滚动走马灯特效插件cxScroll
查看>>
Yii2.0 数据库查询
查看>>
yii2 db 操作
查看>>
mongodb group 有条件的过滤组合个数。
查看>>
关于mongodb的 数组分组 array group
查看>>
MongoDB新的数据统计框架介绍
查看>>