1040: output absolute value
time limit: 1000 ms Memory limit: 65536 KB
[Title Description]
Enter a floating-point number and output the absolute value of the floating-point number to two decimal places.
[input]
Enter a floating point number whose absolute value does not exceed 10000.
[output]
Output the absolute value of this floating-point number to two decimal places.
[input example]
-3.14
[output example]
3.14
explain:
This paper mainly examines the use of if conditional statements to solve the absolute value problem.
The so-called absolute value can be simply understood here as: the absolute value of a positive number is itself, the absolute value of a negative number is its opposite, and the absolute value of 0 is 0.
For example, the absolute value of - 4 is 4, and the absolute value of 4 is 4.
In mathematics, use | x | to represent the absolute value of X. There is no absolute value operator in C/C + +.
Train of thought analysis:
Enter a floating point number, which can be solved by mathematical method or programming method. The following will introduce several methods that beginners should master. Positive gets positive, positive gets negative (negative gets negative), negative gets positive. That is, the formula of multiplying and dividing two numbers. When two positive or negative numbers are multiplied or divided, the result is a positive number, otherwise it is a negative number. For example,-3×(-3)=9,-3×3=-9. A real number multiplied by or divided by 1 equals itself, for example-3×1=-3,4×1=4. So multiply by-1 It's the opposite of itself. This is one of them. And a number plus a minus sign“-",It becomes the opposite number. For example, 3 plus a minus sign becomes-3,and-3 Add a minus sign and it becomes-(-3),And according to negative, negative is positive,-(-3)It's actually 3. This is also a method. The above two methods are mathematical methods. Now let's introduce them C/C++Programming method. C/C++A function that has an absolute value, such as abs,fabs Wait. abs Function for integers, and fabs Used for floating point numbers. So this question should use fabs Function. abs Function sum fabs Header file of function: C Language:<math.h> C++: <cmath> See the answer code for specific usage. In terms of data type, the title requires that the input is a floating point number, which shall not exceed 10000, and shall be retained to two decimal places. about C++Generally speaking, it can be used float Type or double Type, even long double Type. about C In terms of language, although these three types can also be selected, each type corresponds to a different function. This will be explained in detail in extended learning below.
Solution process (method 2):
Declare a variable of type double x.
Enter a number for the variable x.
If variable x is negative, change variable x to positive.
Output the value of variable x.
Extended learning:
Can you ask why you can use three floating-point variables in a fabs function? For example, use float type, double type, long double type.
Because the fabs function is overloaded. If you don't understand the so-called overload, you can simply understand that Fabs has three functions, but the names are fabs. Their function prototypes are (take C++98 Standard as an example):
float fabs(float x);
double fabs(double x);
long double fabs(long double x);
When the passed in variable is of float type, the first fabs function will be called, and the C + + compiler will recognize it automatically. Even started using templates in the C++11 standard.
C99 standard, three floating-point types. For three functions respectively, the function prototype is as follows:
float fabsf(float x);
double fabs(double x);
long double fabsl(long double x);
If the GCC compiler version is too old, some features related to the long double type may not be supported.
The long double type is not recommended in Olympic Games.
The values returned by all the above absolute value functions are accurate without any rounding mode.
Reference code - C + +:
//Method 1
#include <iostream> #include <iomanip> using namespace std; int main() { float x; cin >> x; if (x < 0) x *= -1; // If x is less than (negative), multiply by - 1 to become a positive number cout << fixed << setprecision(2) << x; return 0; }
//Method 2
#include <iostream> #include <iomanip> using namespace std; int main() { double x; cin >> x; if (x < 0) x = -x; // If x is less than 0 (negative), add a negative sign to X to make x positive cout << fixed << setprecision(2) << x; return 0; }
//Method 3
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { long double x; cin >> x; cout << fixed << setprecision(2) << fabs(x); return 0; }
Reference code - C language:
The above three functions will be shown. Note the format characters of different floating-point inputs:
float corresponds to "% f"
double corresponds to '% lf'
long double corresponds to "% Lf"
For printf function: it may be written in the form of "% f" in the previous problem solution, because the long double type was not used before. According to the description of C99 standard, "% f" can be used for float type and double type, but "% Lf" should be used for long double type.
For method 3 in the following code, it is an error after submission, because this version of GCC is not friendly to the support of long double type. Therefore, this is one of the reasons why it is not recommended to use the long double type for the students of Orsay.
//Method 1 (fabsf function of float type)
#include <stdio.h> #include <math.h> int main() { float x; scanf("%f", &x); printf("%.2f", fabsf(x)); return 0; }
//Method 2 (fabs function of double type)
#include <stdio.h> #include <math.h> int main() { double x; scanf("%lf", &x); printf("%.2lf", fabs(x)); return 0; }
//Method 3 (fabsl function of long double type)
#include <stdio.h> #include <math.h> int main() { long double x; scanf("%Lf", &x); printf("%.2Lf", fabsl(x)); return 0; }