練習(xí)2-11 計(jì)算分段函數(shù)[2] (10 分)
1. 題目摘自
https://pintia.cn/problem-sets/12/problems/244
2. 題目?jī)?nèi)容
本題目要求計(jì)算下列分段函數(shù)f(x)的值:
8.jpg
注:可在頭文件中包含math.h
,并調(diào)用sqrt
函數(shù)求平方根,調(diào)用pow
函數(shù)求冪播瞳。
輸入格式:
輸入在一行中給出實(shí)數(shù)x互艾。
輸出格式:
在一行中按“f(x) = result”的格式輸出瞎领,其中x與result都保留兩位小數(shù)怔蚌。
輸入樣例1:
10
輸出樣例1:
f(10.00) = 3.16
輸入樣例2:
-0.5
輸出樣例2:
f(-0.50) = -2.75
3. 源碼參考
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
double x, y;
cin >> x;
if (x > 0)
{
y = sqrt(x);
}
else
{
y = pow(x + 1, 2) + 2 * x + 1 / x;
}
cout << "f(" << fixed << setprecision(2) << x << ") = " << y << endl;
return 0;
}