C语言四个数从小到大排序

不要数组,还没学到数组。
用if语句。

# include <stdio.h>void main (){   int t,a,b,c,d;   printf("请输入4个数;");   

scanf("%d,%d,%d,%d",&a,&b,&c,&d); 

printf("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);  

if(a>b)      {t=a;a=b;b=t;} if(a>b)      {t=a;a=b;b=t;}}                                         

if(a>c) {t=a;a=c;c=t;} if(a>d) {t=a;a=d;d=t;} if(b>c) {t=b;b=c;c=t;} if(b>d) {t=b;b=d;d=t;} if(c>d) {t=c;c=d;d=t;} 

printf("排序结果如下:\n"); printf("%d %d %d %d \n",a,b,c,d);

C语言即中文版的C语言,是一种面向过程的计算机程序设计语言

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-27
#include<stdio.h>
void main()
{
int a,b,c,d,e;
printf("请分别输入四个数:\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("原顺序: %d %d %d %d\n",a,b,c,d);
if(a<b)
{
e=a;
a=b;
b=e;
}
if(a<c)
{
e=a;
a=c;
c=e;
}
if(a<d)
{
e=a;
a=d;
d=e;
}
if(b<c)
{
e=b;
b=c;
c=e;
}
if(b<d)
{
e=b;
b=d;
d=e;
}
if(c<d)
{
e=c;
c=d;
d=e;
}
printf("按小到大排序 %d %d %d %d\n",d,c,b,a);
}
看看复合吗
a中永远是最大值 b是老二 c是老三 d是老四
这是程序的思路本回答被提问者采纳
第2个回答  2011-03-25
这个是昨天写的3个数从大到小,4个数的就不扩展了~
void Sort()
{
int x,y,z;
printf("Pls input three numbers(x,y,z):\n");
scanf("%d,%d,%d",&x,&y,&z);
if(x<=y)
{
//switch the x,y
//tmp = x;
//x = y;
//y = tmp;
if (z>=y)
printf("\nThe sequence is %d,%d,%d",x,y,z);
else
if(z>=x)
printf("\nThe sequence is %d,%d,%d",x,z,y);
else
printf("\nThe sequence is %d,%d,%d",z,x,y);
}
else
if(z>=x)
printf("\nThe sequence is %d,%d,%d",y,x,z);
else
if (z>=y)
printf("\nThe sequence is %d,%d,%d",y,z,x);
else
printf("\nThe sequence is %d,%d,%d",z,y,x);
}
第3个回答  2011-03-25
给你个参考 这个是排3个数字大小的 if语句排列

#include "stdafx.h"

int main(int argc, char* argv[])
{
int a,b,c;
int temp;

scanf("a=%d b=%d c=%d",&a,&b,&c); //输入的时候按a= b= c= 输入

if(a>b)
{
temp=a;

a=b;

b=temp;

}

if(b>c)

{
temp=b;

b=c;

c=temp;
}

if(a>b)

{

temp=a;

a=b;

b=temp;

}

printf("a=%d,b=%d,c=%d\n",a,b,c);

return 0;

}
第4个回答  2011-03-27
楼上的回答都挺到位了呀,难道还有错?
相似回答