c语言中绝对值大小的比较

输入a,b,c三个整数,要求将绝对值最大者打印输出。
2025-03-23 04:35:11
推荐回答(5个)
回答1:

#include
#include
void main()
{
int a = 3, b = -7, c = 1;
int max = a;
if(abs(b) > abs(max) )/*比较a与b的绝对值,找出其较大绝对值*/
{
max = b;
}
if(abs(c) > abs(max))/*比较c绝对值与前面比较出的较大值的大小*/
{
max = c;
}
printf("max:%d\n", max);/*输出其绝对值最大的数*/
}

回答2:

#include
#include

void main()
{
int a = 4, b = -5, c = 3;
int max = a;

if(abs(b) > abs(max) )
{
max = b;
}

if(abs(c) > abs(max))
{
max = c;
}

printf("max:%d\n", max);
}
=====================================

abs
Calculates the absolute value.

int abs( int n );
or

Example

/* ABS.C: This program computes and displays
* the absolute values of several numbers.
*/

#include
#include
#include

void main( void )
{
int ix = -4, iy;
long lx = -41567L, ly;
double dx = -3.141593, dy;

iy = abs( ix );
printf( "The absolute value of %d is %d\n", ix, iy);

ly = labs( lx );
printf( "The absolute value of %ld is %ld\n", lx, ly);

dy = fabs( dx );
printf( "The absolute value of %f is %f\n", dx, dy );
}

Output

The absolute value of -4 is 4
The absolute value of -41567 is 41567
The absolute value of -3.141593 is 3.141593

回答3:

# include "iostream.h"
int max(int a[]){ //求转化后的数组的最大值的函数
int max=a[0];
for(int i=1;i<3;i++){
if(maxmax=a[i];
}
for(i=0;i<3;i++)
if(max==a[i])
return i;
}

void main(){

int a[3],b[3];
int MAX;
cout<<"请输入三个数\n";//输入数据
for(int i=0;i<3;i++)
cin>>a[i];
for(i=0;i<3;i++){ //把负数转化成整数
if(a[i]<0)
b[i]=-a[i];
else b[i]=a[i];
}
MAX=a[max(b)]; //调用函数
cout<<"三个数中绝对值最大的是"<
}

回答4:

回答5:

都平方下,在比较就好了啊!?