当前位置:网站首页>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 ;
边栏推荐
- [semidrive source code analysis] [x9 chip startup process] 26 - LK of R5 safetyos_ INIT_ LEVEL_ Target phase code flow analysis (TP drvier, audio server initialization)
- D1 Nezha Development Board understands the basic startup and loading process
- 上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
- Encryptor and client authenticate with each other
- jupyternotebook有汉字数据库吗。在深度学习中可以识别手写中文吗
- Install RPM package offline using yum
- List of common ACM knowledge points (to be continued)
- Hash tables, sets, maps, trees, heaps, and graphs
- Informatics Olympiad all in one 1000: introductory test questions
- AVFoundation
猜你喜欢

阿里云开发板HaaS510响应UART串口指令

Formal analysis of Woo Lam protocol with scyther tool

Now you must know the pointer

高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法
![[WUSTCTF2020]颜值成绩查询-1](/img/dc/47626011333a0e853be87e492d8528.png)
[WUSTCTF2020]颜值成绩查询-1
Introduction to color coding format

Talk about the top 10 classic MySQL errors

Automatic Generation of Visual-Textual Presentation Layout
FFmpeg 学习指南

Web3.0,「激发创造」的时代
随机推荐
Xcode debugging OpenGLES
Codeforces 1637 B. mex and array - reading, violence
[advanced MySQL] query optimization principle and scheme (6)
Time processing in C language (conversion between string and timestamp)
Alibaba Cloud Development Board haas510 submission Device Properties
618进入后半段,苹果占据高端市场,国产手机终于杀价竞争
1414: [17noip popularization group] score
2021-05-28
Codeforces 1629 E. grid XOR - simple thinking
字节序数据读写
FFmpeg 学习指南
上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
Scyther工具形式化分析Woo-Lam协议
Pytorch framework
[advanced MySQL] evolution of MySQL index data structure (IV)
Fourteen week assignment
Byte order data read / write
When the byte jumps, the Chinese 996 is output in the United States
AWLive 结构体的使用
阿里云开发板HaaS510报送设备属性