日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pow(x, n)

發(fā)布時(shí)間:2025/7/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pow(x, n) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Implement pow(x,?n).

Analyse: To avoid exceeding time, first consider the basic situation, see line5~11; then set some precision s.t. when the difference of result and 0 is less than that precision, return 0;

好的解法:7ms

1 class Solution{ 2 public: 3 double pow(double x, int n){ 4 if(n == 0) return 1; 5 if(x == 1) return 1; 6 if(x == -1) return (n % 2 == 0 ? 1 : -1); 7 8 if(n < 0) 9 return 1.0 / pow(x, -n); 10 else 11 return power(x, n); 12 } 13 private: 14 double power(double x, int n){ 15 if(n == 0) return 1; 16 double temp = power(x, n/2); 17 if(n % 2 == 0) return temp * temp; 18 else return temp * temp * x; 19 } 20 };

?

自己寫的: 32ms

1 class Solution { 2 public: 3 double pow(double x, int n) { 4 double result = 1.00; 5 if(n == 0) return result; 6 if(x == 0) return 0; 7 if(x == 1) return 1; 8 if(x == -1){ 9 if(n % 2 == 0) return 1; 10 else return -1; 11 } 12 13 if(n < 0){ 14 n = -n; 15 x = 1 / x; 16 } 17 18 for(int i = 0; i < n; i++){ 19 result *= x; 20 if(abs(result - 0) < 0.0000000001) return 0; 21 } 22 return result; 23 } 24 };

?

轉(zhuǎn)載于:https://www.cnblogs.com/amazingzoe/p/4437070.html

總結(jié)

以上是生活随笔為你收集整理的Pow(x, n)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。