9-degree OJ1514: the integer power of the value, offeroj1514

Source: Internet
Author: User

9-degree OJ1514: the integer power of the value, offeroj1514

Code Quality-code integrity
Ensure code integrity:

  • Function Testing
  • Border Test
  • Negative Test
    Possible incorrect input
    Error Handling Method:
  • Return Value
  • Global Variables
  • Exception
  • -

Question link:
Http://ac.jobdu.com/problem.php? Pid = 1, 1514

Question 1514: integer power of a value

Time Limit: 1 second memory limit: 128 MB Special Judgment: No submitted: 2051 resolution: 549
Description:
Given a double-Type Floating Point Number base and an int-type Integer exponent. Evaluate the base's exponent power.
Input:
The input may contain multiple test examples.
For each input file, enter an integer T in the first line to indicate the number of test cases. In the next line, enter a floating point base and an integer exponent, separate Two numbers with a space.
Output:
Corresponding to each test case,
Output A floating point number to indicate the answer. Keep the two decimal places.
Sample input:
5
1.0 10
0.0-5
1.0 0
1.2 5
2.0-1
Sample output:
1.00e + 00f
INF
1.00e + 00f
2.49e + 00f
5.00e-01f
Tip:
Pay special attention to the differences in the number of exponential digits in scientific notation output by different compilers. We recommend that you use the compiling environment of jiudu Online Judge.

Train of Thought Analysis:

Test the code integrity.
Main test cases: consider the cases where the upper exponent of various boundary values is 0 and negative. The base number is 0, negative, and so on.
The function algorithm is simple:
-End at the time of O (n) when the for loop is multiplied one by one;
-With the idea of a fast power, the time of the binary O (log (n) is used to end.
The quick power is also the best algorithm in the book. Code:

/******************************** ------------------------------- [Sword refers to the offline interview questions] jiudu OJ1514: the integer power of the value --------------------------------- Author: Muzhi, Date: 2015 Email: bzhou84@163.com **********************************/# include <stdio. h> # include <stdlib. h> # include <string> # include <math. h ># include <stack >#include <iostream> using namespace std; // an error occurs in the input of the global variable control program. bool InvalidInput = false; // evaluate the absolute value of the base, Exp, power doub Le PowerWithabsExp (double base, int Exp) {if (Exp = 0) return 1.0; if (Exp = 1) return base; double result = PowerWithabsExp (base, exp> 1); // Exp divided by 2 result * = result; if (Exp & 1 = 1) // judge Exp parity result * = base; return result ;} // determine whether the double type is equal to bool equal (double numberOne, double numberTwo) {if (numberOne-numberTwo>-0.0000001 & numberOne-numberTwo <0.0000001) {return true ;} else {return false ;}// evaluate the base's Exp power double Power (double base, int Exp) {InvalidInput = false; if (equal (base, 0.0) & Exp <0) {// illegal input InvalidInput = true; return 0.0 ;} int absExp = Exp; if (Exp <0) {absExp =-Exp;} double result = PowerWithabsExp (base, absExp); if (Exp <0) {result = 1.0/result;} return result;} int main () {int n, exp; double base; while (scanf ("% d", & n )! = EOF) {int I; for (I = 0; I <n; I ++) {scanf ("% lf % d", & base, & exp ); double result = Power (base, exp); if (InvalidInput) printf ("INF \ n"); else printf ("%. 2ef \ n ", result) ;}} return 0 ;} /*************************************** * *********************** Problem: 1514 Language: C ++ Result: Accepted Time: 80 MS Memory: 1520 kb ************************************** **************************/
Another method: write with the Boundary Value Driver

I wrote my own ideas, but there is still no beauty in the book.
Various boundary values:
Base:
Positive: normal
Negative: (-base) ^ Exp = (-1) ^ Exp * base ^ Exp
0: 0 negative to the power of meaningless, program error. The positive value of 0 is 0.
Exp:
Positive: normal
Negative number: base ^ (-Exp) = 1/base ^ Exp
0: Zero operators with a non-zero number are all 1. 0 to the power of 0 is controversial. Please refer to the 0 ^ 0 dispute
(0 ^ 0 is equal to 1 in the sword-finger book, but this test case is not found in 9 degrees, so 0 is independent for 1)
All combinations can be written in the boundary value driver.
  

Code:
/******************************** ------------------------------- [Sword refers to the offline interview questions] jiudu OJ1514: the integer power of the value --------------------------------- Author: Muzhi, Date: 2015 Email: bzhou84@163.com **********************************/# include <stdio. h> # include <stdlib. h> # include <string> # include <math. h ># include <stack >#include <iostream> using namespace std; // judge whether the double type is equal to bool equal (double numberOne, double numberTwo ){ If (numberOne-numberTwo>-0.0000001 & numberOne-numberTwo <0.0000001) {return true ;} else {return false ;}} // An error occurred while using the global variable control program. bool InvalidInput = false; double Power (double base, int Exp) {if (Exp = 0) {return 1; // here 0 ^ 0 is 1} else if (Exp <0) {if (equal (base, 0.0) {// illegal input InvalidInput = true; return 0.0 ;} else {return Power (1.0/base,-Exp) ;}} else {if (base <0) {return (Exp & 1? -1: 1) * Power (-base, Exp);} else if (equal (base, 0.0) {// The positive number of 0 is 0 return 0.0 ;} double res = Power (base, Exp/2); // At this time base> = 0, Exp> 0 if (Exp & 1) {return res * base ;} else {return res * res ;}} int main () {int n, Exp, I; double base, result; while (scanf ("% d", & n )! = EOF) {for (I = 0; I <n; ++ I) {scanf ("% lf % d", & base, & Exp); InvalidInput = false; // set InvalidInput to false every time. result = Power (base, Exp); if (InvalidInput) printf ("INF \ n"); else printf ("%. 2ef \ n ", result) ;}} return 0 ;} /*************************************** * *********************** Problem: 1514 Language: C ++ Result: Accepted Time: 80 MS Memory: 1520 kb ************************************** **************************/
Summary:

1. Set whether the global variable ID has an error.
2. judge that the double type is equal and cannot be used directly =
3. The time of the binary O (log (n) end with the idea of a fast power
4. The right shift operator is replaced by 2.
5. bitwise AND 1 Operations replace the remainder operation (%) to judge the parity
6. Details determine success or failure !!!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.