输入三个数求出最大值(5种方法)

论坛 期权论坛 脚本     
匿名技术用户   2021-1-13 05:13   438   0

这是一个很简单的C语言程序,重要的是考验思考问题的角度:

方法1:

#include<stdio.h>

void main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>=b && a>=c)
printf("max=%d\n",a);

else if(b>=a && b>=c)
printf("max=%d\n",b);

else printf("max=%d\n",c);

}

方法2:

#include<stdio.h>

double max3(double x,double y,double z)
{
return (x>y?x:(y>z?y:z));
}

void main()
{
double a,b,c,max;
scanf("%lf%lf%lf",&a,&b,&c);
max=max3(a,b,c);
printf("%lf\n",max);
}

方法3:

#include<stdio.h>

double max3(double x,double y,double z)
{
return (x>y?x:y)>z?(x>y?x:y):z;

}

void main()
{
double a,b,c,max;
scanf("%lf%lf%lf",&a,&b,&c);
max=max3(a,b,c);
printf("%lf\n",max);
}

方法4:

#include<stdio.h>

double max3(double x,double y,double z)
{
double t;
t=x;
if(y>t)
t=y;
if(z>t)
t=z;
return t;
}

void main()
{
double a,b,c,max;
scanf("%lf%lf%lf",&a,&b,&c);
max=max3(a,b,c);
printf("%lf\n",max);
}

方法5:可以利用此法进行输出数据排序,从小到大分别为x,y,z

#include<stdio.h>
double max3(double x,double y,doublez)
{
double t;
if(x>y)
{
t=x;
x=y;
y=t;
}
if(x>z)
{
t=x;
x=z;
z=t;
}
if(y>z)
{
t=y;
y=z;
z=t;
}
return z;
}

void main()
{
double a,b,c,max;
scanf("%lf%lf%lf",&a,&b,&c);
max=max3(a,b,c);
printf("%lf\n",max);
}

方法6

#include<iostream.h>
int main()
{
int x,y,z;
cin>>x>>y>>z;
if(x>y)
if(x>z)
cout<<"max="<<x<<endl;
else
cout<<"max="<<z<<endl;
else
if(z<y)
cout<<"max="<<y<<endl;
else
cout<<"max="<<z<<endl;
return 0;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP