华为机试 - 身高差最小身高

来源:互联网 发布:网络教育文凭国家承认 编辑:程序博客网 时间:2024/04/28 06:38

输入一组身高在170到190之间(5个身高),比较身高差,选出身高差最小的两个身高;若身高差相同,选平均身高高的那两个身高;从小到大输出;

如 输入 170 181173 186 190   输出 170 173

#include <iostream>#include <algorithm>#include <string>using namespace std;int main(){int height[5]={0};for (int i=0;i<5;i++){cin>>height[i];}sort(height,height+5);int difHeig=height[4];int height1,height2;for (int i=1;i<5;i++){int temp=height[i]-height[i-1];if (temp<=difHeig){difHeig=temp;height1=height[i-1];height2=height[i];}}cout<<height1<<' '<<height2<<endl;system("pause");return 0;}


0 0
原创粉丝点击