c语言自练,OJ系统第十七题。
题目描述
本题目要求计算下列分段函数的值:
注:可在头文件中包含math.h,并调用sqrt函数求平方根,调用pow函数求幂。
输入
输入在一行中给出实数x。
输出
在一行中按“f(x) = result”的格式输出,其中x与result都保留两位小数。
样例输入
10
样例输出
f(10.00) = 3.16
其中:sqrt,在有头文件#include<math.h>时可以使用,作用是开平方,在判断素数中也会用到。其他的在1015中有提及,不再赘述。
#include<stdio.h>
#include<math.h>
int main()
{
float x, result;
scanf("%f", &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;
}