用C语言编写程序使一个一维数组内的数从大到小排列

2024-10-29 22:13:19
推荐回答(2个)
回答1:

#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;
}

回答2:

int intTemp; int[] intArray = new int[20]; Random ran = new Random(); for (int i = 0; i < intArray.Length; i++) { int j = ran.Next(1, 1000); intArray[i] = j; } for (int i = 0; i < intArray.Length; i++) { for (int j = 0; j < intArray.Length; j++) { if (intArray[i] > intArray[j]) { intTemp = intArray[i]; intArray[i] = intArray[j]; intArray[j] = intTemp; } } } foreach (int i in intArray) { Console.WriteLine(i); }