6-6 sign function (10 minutes)

Questions required to achieve the signum function sign (x).

Function interface definition:

int sign( int x );

Where x is an integer parameter passed in the user. Sign function is defined as: if x is greater than 0, sign (x) = 1; when x is equal to 0, sign (x) = 0; otherwise, sign (x) = -1.

Referee test program Example:

#include <stdio.h>

int sign( int x );

int main()
{
    int x;

    scanf("%d", &x);
    printf("sign(%d) = %d\n", x, sign(x));

    return 0;
}

/* 你的代码将被嵌在这里 */

Sample input:

10

Sample output:

sign(10) = 1

int sign( int x )
{
	if(x>0)
		return 1;
	else if(x==0)
		return 0;
	else 
		return -1;
}
Published 45 original articles · won praise 26 · views 335

Guess you like

Origin blog.csdn.net/Noria107/article/details/104212404