用C语言编程,读20个整数存入一个数组中,并按从大到小的顺序排列

2024-11-18 17:39:53
推荐回答(2个)
回答1:

#include"stdio.h"
void main(){
int a[20],i,j,temp;
for(i=0;i<20;i++){
printf("输入第%d个数:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<20;i++)
for(j=0;j<20;j++)
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("排序后的:\n");
for(i=0;i<20;i++)
printf("%d\t",a[i]);
}

回答2:

#include
#include
using namespace std;
#define maxn 1000
int a[maxn];
bool cmp ( int a, int b ) {
return a > b;
}int main()
{
int i, n;
printf ( "请输入要排序的元素个数:" );
scanf ( "%d", &n );
printf ( "请依次输入%d个数:", n );
for ( i = 0; i < n; i++ )
scanf ( "%d", &a[i] );
sort ( a, a + n, cmp );
for ( i = 0; i < n; i++ )
printf ( "%3d", a[i] );
return 0;
}