力扣算法题—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 ...
随机推荐
- 学会这个删库再也不用跑路了~ --技术流ken
前言 相信每一个学IT的人或多或少都听说过从删库到跑路这个梗~下图也是在各种交流群屡禁不止,新人听着也是瑟瑟发抖. 人们茶余饭后,街头巷角难免要问... 下面技术流ken就教给各位新手们一招删库再也不 ...
- mybatis是如何防止SQL注入的
mybatis是如何防止SQL注入的 1.首先看一下下面两个sql语句的区别: <select id="selectByNameAndPassword" parameterT ...
- IDEA更换主题
更换IDEA主题只需要3步 1. 下载主题 在主题网站上IDEA Color Themes 上浏览喜欢的主题并下载该主题.(如果网址有变更,google IDEA themes即可.) 2. 导入主 ...
- .net 公共基础类
using WL.Infrastructure.Http; using System; using System.Collections.Generic; using System.IO; using ...
- c#实战开发:以太坊私链搭建(一)
1.第一步环境搭建 运行环境:window 客户端版本:Go语言geth 下载地址https://ethereum.github.io/go-ethereum/downloads/ 以太坊API中文 ...
- vs2010打不开vs2017的.sln文件,出现错误提示 “选择的文件是解决方案文件 但是用此应用程序的较新版本创建的,无法打开”
解决方案: 1.复制下面这段语句 Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 2. ...
- java通过Access_JDBC30读取access数据库时无法获取最新插入的记录
1.编写了一个循环程序,每几秒钟读取一次,数据库中最新一行数据 连接access数据库的方法和查询的信息.之后开一个定时去掉用. package javacommon.util; import jav ...
- 应用分类&练手项目计划
应用分类 练手项目 [应用] 通讯录 xx管理 聊天室 [组件] web容器 db 中间件
- angularJs - cynthia娆墨旧染-响应式文章发布系统
(0)功能 a.添加新文章 b.修改已发布文章 c.搜索已经发布的文章 d.demo链接: http://cynthiawupore.github.io/angularJS (1)界面 a.文章列 ...
- OSI 七层,TCP 四层 , TCP 五层模型介绍
以 TCP 四层模型为例,介绍对应的物理设备 传输层: 四层交换机,四层路由器 网络层: 路由器,三层交换机 数据链路层: 网桥,以太网交换机,网卡 物理层: 中继器,集线器,双绞线 各层功能介绍 物 ...