要从5个人中选取2个人作为礼仪,其中每个人的身高范围为160-190,要求2个人的身高差值最小(如果差值相同的话,选取其中最高的两人),以升序输出两个人的身高。
样例输入:161 189 167 172 188
样例输出: 188 189
#include<iostream>
using namespace std;
const int N= 5;
int a[N];
int main()
{
for(int i=0; i<N; i++)
cin>>a[i];
for(int i=0; i<N; i++)
for(int j=N-1;j>=i+1; j--)
if(a[j]<a[j-1])
{
int t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
int temp=a[1]-a[0];
for(int i=0;i<N-1;i++)
if((a[i+1]-a[i])<temp)
temp=a[i+1] - a[i];
for(int i=N-1;i>=1; i--)
if((a[i]-a[i-1]) == temp)
{
cout<<a[i-1]<<" "<<a[i]<<endl;
break;
}
return 0;
}