Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
从键盘上输入任意一个整数,然后输出它的绝对值!
Input
从键盘上输入任意一个整数。
Output
输出它的绝对值。
Sample Input
-4
Sample Output
4
Hint
Source
本题就是一个简单的选择处理,可以将输入结果分为:大于等于零和小于零。但本题也可以直接用绝对值函数abs();
方法一:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
scanf("%d",&a);
if(a<0)
b=-a;
else b=a;
printf("%d",b);
return 0;
}
-4
4
Process returned 0 (0x0) execution time : 1.200 s
Press any key to continue.
方法二:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
scanf("%d",&a);
printf("%d\n",abs(a));
return 0;
}
-4
4
Process returned 0 (0x0) execution time : 0.910 s
Press any key to continue.