sort (STL)

#include <algorithm>
//仅C++

使用方法:

sort(首指针,尾指针,比较函数)

实例:

输入:

  数据个数

  数据

输出:

  有序数列

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int Maxm = 1000 + 2;
int n[Maxm];

bool cmp(int a, int b) {return a > b;}

int main()
{
    int t; 
    scanf("%d", &t);
    for(int i = 0; i < t; i++)
        scanf("%d", &n[i]);
    sort(n + 1, n + t, cmp);
    for(int i = 0; i < t; i++)
        printf("%d ", n[i]);
    return 0;
}

倒序版本(比较函数自写)  //不是很好,请见谅

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int Maxm = 1000 + 2;
int n[Maxm];

int main()
{
    int t; 
    scanf("%d", &t);
    for(int i = 0; i < t; i++)
        scanf("%d", &n[i]);
    sort(n + 1, n + t);
    for(int i = 0; i < t; i++)
        printf("%d ", n[i]);
    return 0;
}

The End.

原文地址:https://www.cnblogs.com/QQ-1615160629/p/4920681.html