给一个长度为n的数组,输出使得(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn)最大的数组顺序。
先排序,输出最大,输出中间,最后输出最小。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 1005;
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d", &n) != EOF) {
int a[maxn];
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
sort(a, a + n);
printf("%d", a[n - 1]);
for(int i = 1; i < n - 1; ++i)
printf(" %d", a[i]);
printf(" %d\n", a[0]);
}
return 0;
}