1119: 一维数组排序
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3097 Solved: 1620
Submit Status Web Board
Description
对一维数组按照从小到大的顺序排序。程序定义函数sort()来实现数组a的排序。函数原型如下:
int sort(int a[], int n);
数组元素的输出调用PrintArr()。
Input
第一行输入一个整数n(1<=n<=10),表示数组有n个整数;第二行输入n个整数。
Output
输出占一行。对这n个整数数按照从小到大的顺序输出,数据之间用一个空格隔开。
Sample Input
66 5 1 2 3 4
Sample Output
1 2 3 4 5 6
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int a[22];
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+1,a+n+1,cmp);
for (int i=1;i<n;i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[n]);
return 0;
}