C语言 三个数求最大值的不同解法

方法一:

#include <stdio.h>  
  
int compare(int a,int b, int c);  
  
int main()  
{  
        int one,two,three,Max;  
  
        printf("please input three number you want to compare:\n");  
  
        scanf("%d%d%d",&one,&two,&three);  
  
        Max=compare(one,two,three);  
  
        printf("the Max of the [%d %d %d] is %d.\n",one,two,three,Max);  
  
        return 0;  
  
}  
  
int compare(int a,int b, int c)  
{  
        if(a>b)  
                if(a>c)  
                        return a;  
                else  
                        return c;  
        else  
                if(b<c)  
  
                        return c;  
                else  
                        return b;  
}  

方法二:

#include <stdio.h>  
  
int compare(int a,int b, int c);  
  
int main()  
{  
        int a,b,c,Max;  
        printf("please input three number you want to compare:\n");  
        scanf("%d%d%d",& a,& b,& c);  
  
        Max=((a>b)?((a>c)?a:c):((b<c)?c:b));  
  
        printf("the Max of the [%d %d %d] is %d.\n",a,b,c,Max);  
  
        return 0;  
  
} 

两种方法个人更倾向于第一个。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容