【qscoj】哗啦啦村的奇迹果实(一)

来源:互联网 发布:sam软件csp 编辑:程序博客网 时间:2024/05/01 06:55

描述
哗啦啦村生长着一个奇迹树,奇迹树上长着奇迹果实。

传说只要答对奇迹树前牌子的五个问题,就能获得一个奇迹果实。

第一个问题是这样的:

给你n个数,请你选择出两个数,使得这两个数的差值最大

输入
本题包含若干组测试数据
第一行一个n,表示数据的个数。
第二行n个数。

满足,1<=n<=100000,1<=a[i]<=1e9

输出
输出最大的差值。

样例输入1
3
1 2 3

样例输出1
2

A :

#include<bits/stdc++.h>using namespace std;const int maxn = 100005;int a[maxn];int main() {    int n;    while (cin >> n) {        for (int i = 0; i < n; ++i) {            cin>>a[i];        }        sort(a, a + n);        int ans = a[n-1] - a[0];    // 排序后首尾差值最大         cout << ans <<endl;    }    return 0;}

qscqesze : 排序嘛,在ACM/OI竞赛中,我只推崇一种排序方法,就是直接调用algorithm里面的sort函数。
sort() Sorts the elements in the range [first,last) into ascending order.

0 0