@Lin--
2018-10-14T16:08:14.000000Z
字数 1279
阅读 434
Example 1:
Input: 2.00000, 10
Output: 1024.00000
Example 2:
Input: 2.10000, 3
Output: 9.26100
Example 3:
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Note:
-100.0 < x < 100.0
n is a 32-bit signed integer, within the range [−2^31, 2^31-1]
计算机数学
在此输入正文
/**File:pow.c*Author:Hongpei lin*Date:20181014*Purpose:to solve the problem in Leetcode*https://leetcode.com/problems/powx-n/description/*//***Declaration of functions.*Input a double x and integer n*Output x^n*Method:In normal circumstances,make a circulate,let x*x execute n times*In special circumstances,make special method**/double myPow(double x, int n) {double k=1; //k memory the result//if x=1,find that x^n=1if(x==1) return 1.0;//if x=-1,and n is a even,find that x^n=1//if x=-1,and n is an odd,find that x^n=-1if(x==-1){if(n%2==0) return 1.0;else return -1.0;}//if n=0,find that x^n=1if(n==0) return 1.0;//if n=-(2^31),find the result will overflowif(n==-2147483648) return 0.0;else if(n>0){//the process of powwhile(n!=0){//if result is infinitely great,it will overflowif(k==NAN) return 0.0;//if n is a odd,result multiply a longly xif(n%2==1) {k*=x;}//n divide 2n=n/2;//x=x*xx*=x;}}else{//if n<0,let x=1/x,the result is (1/x)^|n|x=1/x;n=-n;while(n!=0){//if result is infinitely small,it will overflowif(k==INFINITY) return 0.0;//the same processif(n%2==1) {k*=x;}n=n/2;x*=x;}}return k;}