美文网首页
C语言 三个数求最大值的不同解法

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

作者: 863cda997e42 | 来源:发表于2018-02-08 13:55 被阅读536次

方法一:

#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;  
  
} 

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

相关文章

网友评论

      本文标题:C语言 三个数求最大值的不同解法

      本文链接:https://www.haomeiwen.com/subject/fcrttftx.html