pow(x,n) leecode
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                pow(x,n) leecode
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                https://oj.leetcode.com/problems/powx-n/?提交地址 快速冪的使用,可以研究一下
1 public class Solution { 2 public double pow(double x, int n) { 3 4 if(n==0) return 1.0; 5 if(x==1) return 1.0; 6 if(x==0)return 0; 7 if(x==-1&&n%2==0) return 1; //有幾個個數據一致通不過,才加入這么多判斷,其實判斷n=0 n>0 n<0, 8 int f=1; 9 if(n<0){ f=-1; n=-n;} 10 double res=1.0; 11 double temp=x; 12 13 while(n!=0) 14 { 15 if((n&1)==1) 16 { 17 res*=temp; 18 } 19 temp=temp*temp; 20 n=n>>1; 21 } 22 23 if(f==1) return res; 24 else return 1.0/res; 25 26 27 28 29 30 } 31 }?
轉載于:https://www.cnblogs.com/hansongjiang/p/3822646.html
總結
以上是生活随笔為你收集整理的pow(x,n) leecode的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 结构体与共用体(联合体)
- 下一篇: tomcat加载出现找不到web
