|
这是一个很简单的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; } |