当前位置:网站首页>After reading the question, you will point to offer 16 Integer power of numeric value
After reading the question, you will point to offer 16 Integer power of numeric value
2022-06-12 13:51:00 【Doll noodles I】
The finger of the sword Offer 16. Integer power of value
The question :
Realization pow(x, n) , Computation x Of n Power function ( namely ,xn). Do not use library functions , At the same time, there is no need to consider the problem of large numbers .
Example 1:
Input :x = 2.00000, n = 10
Output :1024.00000
Example 2:
Input :x = 2.10000, n = 3
Output :9.26100
Example 3:
Input :x = 2.00000, n = -2
Output :0.25000
explain :2-2 = 1/22 = 1/4 = 0.25
Their thinking :
Premise : because x be equal to 2 Of 31 Power -1, So there is no solution with violence . Then we have to solve it by splitting
How to split ?
In fact, it is to change the base number , Because the base becomes larger, the index will correspondingly become smaller , and
If your base number changes every time as a square , Then the index will be divided by each time 2. So the time complexity becomes log2nThe point is to change the base number . Because according to the index , Changes are also different . also
The index may be negative, So there are more cases .
Let's first discuss the case where the index is positive :
- If the exponent is even , So let's just Base squared ( base number * base number ), Then the exponent is divided by 2 that will do
- If the exponent is odd . We need to split , Take out a base number to make the index even . Then record the split number
Specific examples are shown in the figure below :
In the same way, we can divide the exponent by each time 2 To shrink .
In the end, the exponent may be equal to 1, It may also be equal to 2;
- be equal to 1 If so, let's just point to the number divided * The last base
- be equal to 2 So we have to square the base first and then * The number of splits
This part of the code is as follows :
// Greater than 2 In this case, we continue to divide the number of times by 2
while(n > 2)
{
// If it's odd
if(n % 2 == 1)
{
// First split a base number
res *= index;
// Let the index decrease 1
n--;
}
// If the base number is even
else
{
// Base squared , Index divided by 2
index = index * index;
n = n / 2;
}
}
// be equal to 2 Let's square the base first and then * The number of splits
if(n == 2)
return index * index * res;
// be equal to 1 It directly refers to the number split * The last base
else
return res * index;
Then we need to judge that it is less than 0 The situation of .
In fact, the whole is the same , Let's go first x Become the reciprocal , then n Take the absolute value to bring in the above code .
So the overall code is as follows :
class Solution {
public:
double myPow(double x, int n)
{
// Index is 0 Or the base number is 1, Whatever else is 1
if(n == 0 || x == 1.0)
return 1;
// Greater than 0 The situation of
else if(n > 0)
{
// Store the split number
double res = 1;
// Record base
double index = x;
while(n > 2)
{
if(n % 2 == 1)
{
res *= index;
n--;
}
else
{
index = index * index;
n = n / 2;
}
}
if(n == 2)
return res * index * index;
else
return res * index;
}
// Less than 0 The situation of
else
{
// Take the bottom
x = 1.0 / x;
// base number
double index = x;
// Take the absolute value
n = abs(n);
// Store the split number
double res = 1;
while(n > 2)
{
if(n % 2 == 1)
{
res *= index;
n--;
}
else
{
index = index * index;
n = n / 2;
}
}
// Return the result according to the situation
if(n == 2)
return res * index * index;
else
return res * index;
}
}
};
summary
In fact, there are repetitive parts , Making a package may be more beautiful . But I didn't finish it when I was doing it . You can do it if you want . Just make a package for the repeated part ;
边栏推荐
- Application of bit operation in C language
- List of common ACM knowledge points (to be continued)
- Data type conversion and conditional control statements
- C language array and pointer
- 1005: estimation of the earth's population carrying capacity
- 阿裏雲開發板HaaS510報送設備屬性
- Fourteen week assignment
- Implementing pytorch style deep learning framework similartorch with numpy
- 【mysql进阶】查询优化原理与方案(六)
- Scyther工具形式化分析Woo-Lam协议
猜你喜欢

聊聊MySQL的10大经典错误

Talk about the top 10 classic MySQL errors

When the byte jumps, the Chinese 996 is output in the United States

单总线温度传感器18B20数据上云(阿里云)

Greed issues - Egypt scores
颜色编码格式介绍

Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation

Alibaba Cloud Development Board haas510 submission Device Properties

Web3.0, the era of "stimulating creativity"
![2061: [example 1.2] trapezoidal area](/img/83/79b73ca10615c852768aba8d2a5049.jpg)
2061: [example 1.2] trapezoidal area
随机推荐
"Non" reliability of TCP
【SemiDrive源码分析】【X9芯片启动流程】26 - R5 SafetyOS 之 LK_INIT_LEVEL_TARGET 阶段代码流程分析(TP Drvier、Audio Server初始化)
GPUImage链式纹理的简单实现
Record some settings for visual studio 2019
Go language functions as parameters of functions
Tree reconstruction (pre order + middle order or post order + middle order)
xcode 调试openGLES
Briefly describe the difference between CGI and fastcgi
聊聊MySQL的10大经典错误
Talk about the top 10 classic MySQL errors
Web3.0, the era of "stimulating creativity"
播放器屏幕方向方案
Top 10 tips for visual studio code on Google
MySQL 查询 limit 1000,10 和 limit 10 速度一样快吗? 深度分页如何破解
Encryptor and client authenticate with each other
D1 哪吒开发板 了解基本的启动加载流程
当字节跳动在美国输出中国式 996
Introduction to color coding format
Pytorch to onnx, onnxruntime reasoning in mmclas
Codeforces 1638 B. odd swap sort - tree array, no, simple thinking