Leetcode_49pow [Pow (x, n)]

Source: Internet
Author: User

# Pragma warning (Disable: 4996) # include <cstdio> # include <tchar. h> # include <windows. h>/* submit time: 31. runtime error34.00515,-32. runtime error1.00000,-2147483648 request: Implement POW (x, n ). */Double powrecursively (Double X, int N) {If (n = 0) return 1; if (n = 1) return X; If (n = 2) return x * X; If (N & 0x1) {double temp = powrecursively (x, (n-1)> 1); Return x * temp ;} else {double temp = Powrecursively (x, N> 1); Return temp * temp;} double POW (Double X, int N) {If (n =-2147483647-1) return 1.0/x * POW (x, n + 1); int Sign = n <0? -1: 1; n = n <0? -N: N; return Sign = 1? Powrecursively (x, n): 1.0/powrecursively (x, n );} // ============================ test ============================ void Test (Double X, int N) {double result = POW (x, n); printf ("Pow (% lf, % d) is: % lf \ n", X, n, result);} void test1 () {Double X = 3.1415; test (x, 5);} void Test2 () {Double X = 3.1415; test (x, 50 );} void test3 () {Double X = 34.00515; test (x,-3) ;}void test4 () {Double X = 1.00000; test (x,-2147483647-1 );} int _ tmain (INT argc, _ tchar * argv []) {test1 (); Test2 (); test3 (); test4 (); System ("pause "); return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.