实验2-2-4 计算分段函数[2](10 分)

实验2-2-4 计算分段函数[2](10 分)

本题目要求计算下列分段函数f(x)的值:
$$
f(x)=\begin{cases}x^{0.5}, & \text{$x \ge 0$时} \\\(x+1)^2+2x+\frac{1}{x}, & \text{$x \lt 0$时} \end{cases}
$$
注:可在头文件中包含math.h,并调用sqrt函数求平方根,调用pow函数求幂。

输入格式:

输入在一行中给出实数x。

输出格式:

在一行中按“f(x) = result”的格式输出,其中x与result都保留两位小数。

输入样例1:

1
10

输出样例1:

1
f(10.00) = 3.16

输入样例2:

1
-0.5

输出样例2:

1
f(-0.50) = -2.75
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<math.h>
int main(void){

double x,re;
scanf("%lf",&x);
if(x >= 0){
re=pow( x, 0.5 );
}else{
re=pow( x + 1,2 ) + 2 * x + 1.0 / x;
}
printf("f(%.2f) = %.2f",x,re);
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×