这是一个求值问题,具体如下:
1.对于一个降序排序序列 a ,对其相邻两个数 ai 与 ai+1,求差 ei (小数减大数)并输出最小值。
2.对于一个非降序排序数列 a ,输出其序列最大值与最小值的差。
代码如下:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
long long a[200010];
long long b[200010];
bool cmp(long long x,long long y)
{
return abs(x)<abs(y);
}
int main()
{
// freopen("money.in","r",stdin);
// freopen("money.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
bool x=true;
for (int i=0;i<n;i++)
{
cin>>a[i];
if (i>=1&&x==true)
{
x=(a[i-1]>a[i]);
b[i]=a[i]-a[i-1];
}
}
if (x==true)
{
nth_element(b,b+2,b+n);
cout<<b[1]<<'\n';
return 0;
}
nth_element(a,a+1,a+n);
long long xx=a[0];
nth_element(a,a+n,a+n);
cout<<a[n-1]-xx<<'\n';
return 0;
}
该程序不能输出正确结果,想请教一下,哪里有问题?