PAT数列排序

19考研结束了

。。

还有11天PAT甲

题目链接:http://lx.lanqiao.cn/problem.page?gpid=T52

题目大意:训练排序

解题思路:

直接用C++里的sort函数,

1  #include<algorithm>
2  using namespace std;

这两句缺一不可。sort函数的排序不仅仅只用了快速排序,还结合了插入排序和堆排序。然后sort函数原型有三个参数:

1.要排序的数组的起始地址。

扫描二维码关注公众号,回复: 5199535 查看本文章

2.结束的地址(最后一位要排序的地址)

3.排序的方法。如果省略是默认了从小到大排序。如果从大到小就要自己写https://blog.csdn.net/latte_z/article/details/17049479

代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     int n;
 7     int a[500];
 8     scanf("%d", &n);
 9     for(int i = 0; i < n; i ++)
10         scanf("%d", &a[i]);
11     sort(a, a + n);
12     for(int i = 0; i < n - 1; i ++)
13         printf("%d ", a[i]);
14     printf("%d\n", a[n - 1]);
15 }

***************************************************只学了皮毛,后续逐渐补充*****************************************

猜你喜欢

转载自www.cnblogs.com/gerjcs/p/10394699.html
今日推荐