要從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;
}