This question requires calculating f(x)
the value of the following piecewise function :
f (x) = {x 0.5 x>=0 (x + 1) 2 + 2 x + 1 / xx<0 f(x)=\begin{cases} x^ {0.5}& \text{x>=0}\\ (x+1)^{2}+2x+1/x& \text{x<0} \end{cases}f(x)={
x0.5(x+1)2+2 x+1/xx>=0x<0
Note: It can be included in the header file math.h
, and call the sqrt
function to find the square root, and call the pow
function to find the exponentiation.
Input format:
The input gives the real number x in one line.
Output format:
“f(x) = result”
Output in the format in one line , in which x and result both retain two decimal places.
Input example 1:
10
Output example 1:
f(10.00) = 3.16
Input example 2:
0.5
Output example 2:
f(-0.50) = -2.75
Code:
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
int main(){
double x,result;
scanf("%lf",&x);
if (x >= 0) result = sqrt(x);
else result = pow(x+1,2) + 2 * x + 1.0 / x;
printf("f(%.2lf) = %.2lf",x,result);
return 0;
}
Submit screenshot:
Problem-solving ideas:
It is mainly math.h
the use of the library. Importing this library can use some packaged ready-made mathematical formulas, common ones sqrt
, pow
etc.