程式碼示例:
方法一:用if else語句求解
#include<stdio.h>
int main()
{
int a,b,c,x;
scanf("%d %d %d",&a,&b,&c);
if(a>b){x=a;a=b;b=x;}
if(a>c){x=a;a=c;c=x;}
if(b>c){x=b;b=c;c=x;}
printf("%d %d %d\n",a,b,c);
return 0;
}
方法二:用陣列求解
int a,b,c,t;
printf("請輸入三個整數:\n");
scanf("%d\t%d\t%d", &a, &b, &c);
if (a > b)
t = a;
a = b;
b = t;
if (a > c)
a = c;
c = t;
if (b > c)
t = b;
b = c;
printf("這三個數中最大的數是:%d\n", c);
printf("將這三個數按從大到小的順序排列為:%d\t%d\t%d\n", c, b, a);
程式碼示例:
方法一:用if else語句求解
#include<stdio.h>
int main()
{
int a,b,c,x;
scanf("%d %d %d",&a,&b,&c);
if(a>b){x=a;a=b;b=x;}
if(a>c){x=a;a=c;c=x;}
if(b>c){x=b;b=c;c=x;}
printf("%d %d %d\n",a,b,c);
return 0;
}
方法二:用陣列求解
#include<stdio.h>
int main()
{
int a,b,c,t;
printf("請輸入三個整數:\n");
scanf("%d\t%d\t%d", &a, &b, &c);
if (a > b)
{
t = a;
a = b;
b = t;
}
if (a > c)
{
t = a;
a = c;
c = t;
}
if (b > c)
{
t = b;
b = c;
c = t;
}
printf("這三個數中最大的數是:%d\n", c);
printf("將這三個數按從大到小的順序排列為:%d\t%d\t%d\n", c, b, a);
return 0;
}