刷题遇到的各种 之 绝对值

方法一:用C语言中自带的绝对值函数表示:

如果a是整数   abs()   

#include<stdio.h>
#include<math.h>
int a=100,b;
b=abs(a);
printf("%d",b);

如果a是浮点数:    double型     fabs()  

#include<stdio.h>
#include<math.h>
float a=99.9float b;
b=fabs(a);
printf("%f",b);

  

方法二:自己编写一个函数表示:

#include <stdio.h>
int abs(int t)
{
    if (t>0)
        return t;
    else
        return -t;
}
int main()
{
    int t = 0;
    scanf("%d",&t);
    printf("%d",abs(t));
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/expedition/p/11626847.html