1.计算分段函数[1]
题目:本题目要求计算下列分段函数f(x)的值:y=f(x)={1x x≠00 x=0 y=f(x) = \begin{cases} \frac{1}{x}\,\,&x\neq0\\ 0\,\,&x=0\\ \end{cases} y=f(x)={x10x=0x=0
输入格式:
输入在一行中给出实数x。
输出格式:
在一行中按“f(x) = result”的格式输出,其中x与result都保留一位小数。
输入样例1:
10
输出样例1:
f(10.0) = 0.1
输入样例2:
0
输出样例2:
f(0.0) = 0.0
代码1:
#include<stdio.h>
int main()
{
double x,result;
scanf("%lf",&x);
if(x!=0)
{
result=1/x;
}
else
{
result=0;
}
printf("f(%.1f) = %.1f",x,result);
return 0;
}
代码2:
#include<stdio.h>
int main()
{
double x,y;
scanf("%lf",&x);
if(x==0)
y=0;
else
y=1/x;
printf("f(%.1f) = %.1f\n",x,y);
return 0;
}
2.计算分段函数[2]
题目:本题目要求计算下列分段函数f(x)的值:
f(x)={ (x)0.5 (x≥0时)(x+1)2+2x+1x (x<0时)f(x) =
\begin{cases}
\ (x)^{0.5} \,&(x\geq0时)\\
(x+1)^2 + 2x+ \frac{1}{x}\,\,&(x<0时)\\
\end{cases}
f(x)={ (x)0.5(x+1)2+2x+x1(x≥0时)(x<0时)
注:可在头文件中包含math.h,并调用sqrt函数求平方根,调用pow函数求幂。
输入格式:
输入在一行中给出实数x。
输出格式:
在一行中按“f(x) = result”的格式输出,其中x与result都保留两位小数。
输入样例1:
10
输出样例1:
f(10.00) = 3.16
输入样例2:
-0.5
输出样例2:
f(-0.50) = -2.75
代码1:
#include<stdio.h>
#include<math.h>
int main(void)
{
double x,result;
scanf("%lf",&x);
if(x>=0)
{
result=sqrt(x);
}
else
{
result=pow(x+1,2)+2*x+1/x;
}
printf("f(%.2f) = %.2f",x,result);
return 0;
}
代码2:
#include<stdio.h>
#include<math.h>
int main()
{
double x,y;
scanf("%lf",&x);
if(x>=0)
y=sqrt(x);
else
y=pow(x+1,2)+2*x+1/x;
printf("f(%.2f) = %.2f\n",x,y);
return 0;
}