用C语言编写程序使一个一维数组内的数从大到小排列
2个回答
2013-11-14
展开全部
#include<stdio.h>
#include<algorithm>
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;
}
#include<algorithm>
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;
}
推荐于2018-04-12
展开全部
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); }
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询