Exercise 2-11 of the title set of "C Language Programming (3rd Edition)" of Zhejiang University

Exercise 2-11 Calculate the piecewise function [2] (10 points)

This topic requires calculating the value of the following piecewise function f(x):
Insert picture description here
Note: You can include math.h in the header file, and call the sqrt function to find the square root, and call the pow function to exponentiate.
Input format:
Input a real number x in one line.
Output format: output in the format of
"f(x) = result" in one line, where x and result both retain two decimal places.
Input example 1:
10
Output example 1:
f(10.00) = 3.16
Input example 2:
-0.5
Output example 2:
f(-0.50) = -2.75
Author
Chen Jianhai
Unit
Zhejiang University

Code length limit
16 KB
Time limit
400 ms
memory limit
64 MB

#include <stdio.h>
#include <math.h>

int main() {
    
    
    double a;
    if (scanf("%lf", &a) == 1) {
    
    
        if (a >= 0) {
    
    
            printf("f(%.2f) = %.2f", a, pow(a, 0.5));
        } else {
    
    
            printf("f(%.2f) = %.2f", a, (a + 1) * (a + 1) + 2 * a + 1.0 / a);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/DoMoreSpeakLess/article/details/109249412