美文网首页
时间复杂度O(logn),空间复杂度O(1)求一个数double

时间复杂度O(logn),空间复杂度O(1)求一个数double

作者: 贰拾贰画生 | 来源:发表于2017-04-20 21:38 被阅读27次
#include <iostream>

using namespace std;

double f(double x, int n);

int main(){
    
    cout<<f(3, 6)<<endl;
    return 0;
}

double f(double x, int n){
    double res = 1;
    while (n != 0) {
        if(n & 1) res *= x;
        x *= x;
        n = n >> 1;
    }
    return res;
}

相关文章

  • 快速排序 by Python

    最好时间复杂度:O(n*logn)最坏时间复杂度:O(n²)平均时间复杂度:O(n*logn)空间复杂度:O(1)...

  • 归并排序 by Python

    最好时间复杂度:O(n*logn)最坏时间复杂度:O(n*logn)平均时间复杂度:O(n*logn)空间复杂度:...

  • 算法训练营-第一周-数组链表

    一.时间复杂度&空间复杂度 常见的时间复杂度 常量 O(1) 对数 O(logn) 线性 O(n...

  • 时间复杂度

    时间复杂度o(1), o(n), o(logn), o(nlogn) 1、时间复杂度o(1), o(n), o(l...

  • P75-求n次方

    O(logn)时间复杂度求Fibonacci数列

  • 10.算法设计思想之"分而治之"

    时间复杂度 O(logN) && 空间复杂度 O(n) LeeCode:226.翻转二叉树 时间复杂度 O(n) ...

  • 复杂度

    常见复杂度 1.时间复杂度 O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) <...

  • 快排

    复杂度 时间复杂度O(nlogn) 空间复杂度O(logn) 前置知识 荷兰国旗https://www.jians...

  • 复杂度

    1. 时间复杂度 常见时间复杂度高低 O(1) < O(logn) < O(n) < O(nlogn) < O(l...

  • 总结八

    时间复杂度 由上图,可见,O(1) 最小,O(logn) 次之,比如二分查找就是 O(logn) 时间复杂度可见 ...

网友评论

      本文标题:时间复杂度O(logn),空间复杂度O(1)求一个数double

      本文链接:https://www.haomeiwen.com/subject/sacrzttx.html