2或3個整數按從小到大排序並輸出
阿新 • • 發佈:2019-02-06
問題1:由鍵盤輸入x,y兩個數,要求從小到大排序並輸出
#include <stdio.h>
int main()
{
float x, y, t;
scanf("%f%f", &x, &y);
if (x < y)
{
t = x;
x = y;
y = t;
}
printf("%6.2f, %6.2f", x, y);//佔6列,有2位小數,左端補空格。小數點也佔一列
return 0;
}
問題2:輸入3個整數,按由小到大的順序輸出
#include <stdio.h> int main() { int a, b, c, t; printf("input three integer a,b,c:"); scanf("%d%d%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("now the order is:%d, %d, %d\n", a, b, c); return 0; }