Brush Problem Squad-Sword Point offer (ii)

Source: Internet
Author: User

11. Integer number of values

1#include <iostream>2 3 intError =0;4 DoublePower (Double Base,intexponent);5 6 intMainintargcChar*argv[]) {7   Double Base;8   intexponent;9Std::cin >>Base>>exponent;Ten   One   Doubleresult = Power (Base, exponent); A    -Std::cout << Error <<Std::endl; -Std::cout << Result <<Std::endl; the } -  - DoublePower (Double Base,intexponent) { -   if(0==exponent) +     return 0; -   if(Exponent >0) {//the index is positive +     if(1==exponent) A       return Base; at     if(Exponent%2==0) {//even -       DoubleTMP = Power (Base, exponent/2); -       returntmp*tmp; -}Else{//Odd -       DoubleTMP = Power (Base, (exponent-1)/2); -       returntmp*tmp*Base; in     } -}Else{//index is negative to     if(Base==0) { +Error =-1; -       return 0; the     } *     if(-1==exponent) $       return 1/Base;Panax Notoginseng     if(Exponent%2==0) {//even -       DoubleTMP = Power (Base, exponent/2); the       returntmp*tmp; +}Else{//Odd A       DoubleTMP = Power (Base, (exponent+1)/2); the       returntmp*tmp* (1/Base); +     } -   } $}
View Code

*book:

1#include <iostream>2 3 BOOLError =false;4 DoublePower (Double Base,intexponent);5 DoublePositivepower (Double Base, unsignedintexponent);6 BOOLEqualDoubleMDoublen);7 8 intMainintargcChar*argv[]) {9   Double Base;Ten   intexponent; OneStd::cin >>Base>>exponent; A   -   Doubleresult = Power (Base, exponent); -    theStd::cout << Error <<Std::endl; -Std::cout << Result <<Std::endl; - } -  + DoublePower (Double Base,intexponent) { -   //Base is 0, exponent is negative/0/positive +   if(Equal (Base,0.0)) { A     if(Exponent <0) { atError =true; -       return 0; -}Else -       return 0; -   } -  in   //exponent is positive/0 -UnsignedintAbsexponent = (unsignedint) exponent; to   if(Exponent <0) { +Absexponent = (unsignedint)(-exponent); -   }  the   Doubleresult = Positivepower (Base, absexponent); *   return(Exponent <0) ?1/Result:result; $ }Panax Notoginseng  - BOOLEqualDoubleMDoubleN) { the   if(M-n <0.0000001&& m-n >-0.0000001) +     return true; A   return false; the } +  - DoublePositivepower (Double Base, unsignedintexponent) { $   if(Exponent = =1)  $     return Base; -   if(Exponent = =0) -     return 0; the   Doubleresult = Positivepower (Base, exponent >>1); -Result *=result;Wuyi   if(Exponent &0x1==1)//Odd theResult *=Base; -   returnresult; Wu}
View Code

Idea: The nature of using the exponentiation

The subject seems simple, but the perfect writing procedure needs to be considered comprehensively:

1) The base/exponent is 0, negative, positive; (A's N power is the reciprocal of A's-N power, which can be used to simplify the program.)

2) The method of base judgment is 0 (double base== 0 error);

Brush Problem Squad-Sword Point offer (ii)

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.