一个分段函数,返回为double值,保留两位小数。

输入5 输出15.00
这题不难,重要的是sqrt(),fabs(),pow(n,2);
cmath或者math.h里有的sqrt() 平方根函数,fabs()绝对值函数,pow(n,5); nの五次方函数
- #include
- #include
//小数 - #include
- using namespace std;
-
- double fx(int x) {
- double res;
- if (x < 0) {
- res = sqrt(x * x);
- }
- else if (x >= 0 && x < 2) {
- res = sqrt(x + 1);
- }
- else if (x >= 2 && x < 4) {
- res = pow(x + 2, 5);
- }
- else if (x >= 4) {
- res=2 * x + 5;
- }
- return res;
- }
-
- int main() {
- int x;
- cin >> x;
- cout<
setprecision(2)<<fx(x); -
- return 0;
- }