Exercise 3-2 of Zhejiang University Edition "C Language Programming (3rd Edition)"

Exercise 3-2 Calculate the value of the sign function (10 points)

For any integer n, the sign function sign(n) is defined as follows:
Insert picture description here
Please write a program to calculate the value of this function for any input integer.
Input format:
Input gives the integer n in one line.
Output format:
output the function value corresponding to the integer n in one line according to the format "sign(n) = function value".
Input example 1:
10
Output example 1:
sign(10) = 1
Input example 2:
0
Output example 2:
sign(0) = 0
Input example 3:
-98
Output example 3:
sign(-98 ) = -1
Author
C course group
Unit
Zhejiang University
Code length limit

16 KB
Time limit
400 ms
Memory limit
64 MB

#include <stdio.h>

int main() {
    
    
    int a;
    if (scanf("%d", &a) == 1) {
    
    
        if (a < 0) {
    
    
            printf("sign(%d) = -1", a);
        } else if (a == 0) {
    
    
            printf("sign(%d) = 0", a);
        } else {
    
    
            printf("sign(%d) = 1", a);
        }

    }
    return 0;
}

Guess you like

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