给,用if作有些复杂:
#include
#include
void swap(int &a,int &b)
{
a=a+b;
b=a-b;
a=a-b;
}
int main()
{
int a[4]={0};
int rk=0,lk=0;
int i;
printf("please input the number: \n");
for(i=0;i<4;i++)
{
fflush(stdin);
scanf("%d",&a[i]);
}
if(a[rk] swap(a[rk],a[lk]);
if(a[0] swap(a[0],a[2]);
if(a[0] swap(a[0],a[3]);
if(a[1] swap(a[1],a[2]);
if(a[1] swap(a[1],a[3]);
if(a[2] swap(a[2],a[3]);
for(i=0;i<4;++i)
printf("%d ",a[i]);
getch();
return 0;
}
最简单的源代码:
#include"stdio.h"
void main()
{int a[3];
int i,j,t;
printf("请输入四个整数:\n");
for (i=0;i<=3;i++)
scanf("%d",&a[i]);
for(j=0;j<=2;j++)
for(i=0;i<=2-j;i++)
if (a[i]<=a[i+1])
{t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
printf("排序后为:");
for(i=0;i<=3;i++)
printf("%d",a[i]);
printf("\n");
}
#include
main()
{
int a,b,c,d;
int m1,m2,m3,m4;
printf("please input four numbers: ");
scanf("%d %d %d %d",&a,&b,&c,&d);
m1=m2=m3=m4=a;
if(b>m1)
m1 = b;
else
m2 = b;
if(c>m1)
{
m3 = m2;
m2 = m1;
m1 = c;
}
else if(c>m2)
{
m3 = m2;
m2 = c;
}
else
m3 = c;
if(d>m1)
{
m4 = m3;
m3 = m2;
m2 = m1;
m1 = d;
}
else if(d>m2)
{
m4 = m3;
m3 = m2;
m2 = d;
}
else if(d>m3)
{
m4 = m3;
m3 = d;
}
else
m4 = d;
printf("%d %d %d %d\n",m1,m2,m3,m4);
getchar();
}
#include"stdio.h"
main()
{
int a,b,c,d,t;
printf("please input four Integer: ");
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a{
t=b;
b=a;
a=t;
}
if(a
t=c;
c=a;
a=t;
}
if(a
t=d;
d=a;
a=t;
}
if(b
t=c;
c=b;
b=t;
}
if(b
t=b;
b=d;
d=t;
}
if(c
t=d;
d=c;
c=t;
}
printf("%d,%d,%d,%d",a,b,c,d);
}
TC编译成功
在VC中编译通过,你可以改成输入很多个数,按大小输出:
#include
void main()
{
int a[4];
int i,j,t;
for(i=0;i<4;i++)
{
cin>>a[i];
}
for(i=0;i<3;i++)
for(j=i+1;j<4;j++)
{
if(a[i] {
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
for(i=0;i<4;i++)
{
cout< }
}