C语言实现符号函数

您所在的位置:网站首页 sigh函数 C语言实现符号函数

C语言实现符号函数

2024-07-09 14:34| 来源: 网络整理| 查看: 265

浙大版PTA 习题5-1 符号函数

题目要求:本题要求实现符号函数sign(x)。

函数接口定义:

int sign( int x );

其中x是用户传入的整型参数。符号函数的定义为:若x大于0,sign(x) = 1;若x等于0,sign(x) = 0;否则,sign(x) = −1。

裁判测试程序样例: #include int sign( int x ); int main() { int x; scanf("%d", &x); printf("sign(%d) = %d\n", x, sign(x)); return 0; } /* 你的代码将被嵌在这里 */

运行代码:

#include int sign( int x ); int sign( int x ){ if (x>0) return 1; else if(x==0) return 0; else return -1; } int main() { int x; scanf("%d", &x); printf("sign(%d) = %d\n", x, sign(x)); return 0; }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3