力扣算法题—050计算pow(x, n)
#include "000库函数.h" //使用折半算法 牛逼算法
class Solution {
public:
double myPow(double x, int n) {
if (n == 0)return 1;
double res = 1.0;
for (int i = n; i != 0; i /= 2) {
if (i % 2 != 0)
res *= x;
x *= x;
}
return n > 0 ? res : 1 / res;
} }; //同样使用二分法,但是使用递归思想
class Solution {
public:
double myPow(double x, int n) {
if (n == 0)return 1;
double res = myPow(x, n / 2);
if (n % 2 == 0)return x * x;//是偶数,则对半乘了之后再两个大数相乘,x^n==(x^n/2)^2
if (n > 0) return res * res * x;
return res * res / x;
}
}; void T050() {
Solution s;
double x;
int n;
x = 0.0001;
n = 2147483647;
cout << s.myPow(x, n) << endl;
x = 2.1;
n = 3;
cout << s.myPow(x, n) << endl;
x = 2;
n = -2;
cout << s.myPow(x, n) << endl;
x = 0.9;
n = 2147483647;
cout << s.myPow(x, n) << endl; }
力扣算法题—050计算pow(x, n)的更多相关文章
- 力扣算法题—069x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- 力扣算法题—051N皇后问题
#include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...
- 力扣算法题—147Insertion_Sort_List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 力扣算法题—149Max Points on a line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 力扣算法题—093复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- 力扣算法题—079单词搜索【DFS】
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...
- 力扣算法题—052N皇后问题2
跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...
- 力扣算法题—143ReorderList
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...
随机推荐
- python安装第三方库的最简单方式
一.准备工作 (只做一次准备工作,以后都会很方便) 1. 安装pip (1)下载pip到D:\download pip下载地址:https://pypi.python.org/pypi/pip#dow ...
- [转]angular 监听窗口滚动
本文转自:https://blog.csdn.net/ittvibe/article/details/80060801 转自:http://brianflove.com/2016/10/10/angu ...
- .NET CORE 设置cookie以及获取cookie
使用我这个方式的前提是在mvc中,确认你安装了:Microsoft.AspNetCore.Mvc. 然后在继承了Controller的类型中使用我所说的方法. 直接使用即可,我是封装了方法供我自己使用 ...
- JQuery官方学习资料(译):遍历
一旦你通过JQuery创建了选择器,你就可以对此进行更为深入的遍历.遍历可以分为三个基本组成部分,父节点.子节点和兄弟节点.JQuery为这些部分提供了许多丰富易用的方法. <div c ...
- C#隐藏与显示系统任务栏和开始菜单栏按钮
隐藏与显示系统任务栏和开始菜单栏按钮:直接上代码: private const int SW_HIDE = 0; //隐藏 private const int SW_REST ...
- SQL 語法
查詢 Sql = ("SELECT A1, A2, A5, A4 FROM Table1 ") 筆數 Sql = ("Select COUNT(*) From TW01. ...
- spring_08aop原理及案例
*参考优质文档: https://www.cnblogs.com/xrq730/p/4919025.html 一.简介 aop(Aspect Oriented Programming)是面向切面编程, ...
- Java集合类根接口:Collection 和 Map
前言 在前文中我们了解了几种常见的数据结构,这些数据结构有着各自的应用场景,并且被广泛的应用于编程语言中,其中,Java中的集合类就是基于这些数据结构为基础. Java的集合类是一些非常实用的工具类, ...
- Java 学习笔记 执行外部命令 包装类 枚举类型
执行外部命令 Runtime只能通过静态方法getRuntime获得,可以用来执行外部的命令 Runtime runtime = Runtime.getRuntime(); runtime.exec( ...
- openjudge------ 日期的种类题目
描述TXT is a vegetable chicken,so 出题什么的完全不会啊! 干脆直接从网络上copy一题下来吧. 小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都 ...