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

2018-02-08  本文已影响536人  863cda997e42

方法一:

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

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

上一篇下一篇

猜你喜欢

热点阅读