华为-on练习--身高找到最好的二人
2015-10-02 12:20
204 查看
称号:
离5个人选择2个人作为礼工具。中的每个个体的身高的范围160-190,要求2个人高差值至少(假设差异值一样,他们中最高的选择)。输出的两个人的身高升序。
Smple input:161 189 167 172 188 Sample outPut: 188 189
分析:我的理解就是先逆序排好数值, 然后逐对照较身高差值。 找出身高差值最小的然后输出
代码例如以下:
package com.wenj.test;
/**
* 要从5个人中选取2个人作为礼仪。当中每一个人的身高范围为160-190,要求2个人的身高差值最小(假设差值同样的话,选取当中最高的两人)。以升序输出两个人的身高。
* Smple input:161 189 167 172 188 Sample outPut: 188 189
* @author wenj91-PC
*
*/
public class TestBestGround {
public static void main(String args[]){
String strIn = "161 189 167 172 188";
TestBestGround tb = new TestBestGround();
tb.printTheBestGround(strIn);
}
public void printTheBestGround(String strIn){
String strTemp = strIn;
String[] strArr = strTemp.split(" ");
int[] numArr = new int[strArr.length];
for(int i=0; i<strArr.length; i++){
numArr[i] = Integer.parseInt(strArr[i]);
}
for(int i=0; i<numArr.length; i++){
for(int j=i+1; j<numArr.length; j++){
if(numArr[i]<numArr[j]){
int temp = numArr[i];
numArr[i] = numArr[j];
numArr[j] = temp;
}
}
}
int aver = numArr[0]-numArr[1];
int pos = 0;
for(int i=1; i<numArr.length-1; i++){
int temp = numArr[i]-numArr[i+1];
if( temp < aver){
aver = temp;
pos = i;
}
}
System.out.println(numArr[pos+1] + " " + numArr[pos]);
}
}
离5个人选择2个人作为礼工具。中的每个个体的身高的范围160-190,要求2个人高差值至少(假设差异值一样,他们中最高的选择)。输出的两个人的身高升序。
Smple input:161 189 167 172 188 Sample outPut: 188 189
分析:我的理解就是先逆序排好数值, 然后逐对照较身高差值。 找出身高差值最小的然后输出
代码例如以下:
package com.wenj.test;
/**
* 要从5个人中选取2个人作为礼仪。当中每一个人的身高范围为160-190,要求2个人的身高差值最小(假设差值同样的话,选取当中最高的两人)。以升序输出两个人的身高。
* Smple input:161 189 167 172 188 Sample outPut: 188 189
* @author wenj91-PC
*
*/
public class TestBestGround {
public static void main(String args[]){
String strIn = "161 189 167 172 188";
TestBestGround tb = new TestBestGround();
tb.printTheBestGround(strIn);
}
public void printTheBestGround(String strIn){
String strTemp = strIn;
String[] strArr = strTemp.split(" ");
int[] numArr = new int[strArr.length];
for(int i=0; i<strArr.length; i++){
numArr[i] = Integer.parseInt(strArr[i]);
}
for(int i=0; i<numArr.length; i++){
for(int j=i+1; j<numArr.length; j++){
if(numArr[i]<numArr[j]){
int temp = numArr[i];
numArr[i] = numArr[j];
numArr[j] = temp;
}
}
}
int aver = numArr[0]-numArr[1];
int pos = 0;
for(int i=1; i<numArr.length-1; i++){
int temp = numArr[i]-numArr[i+1];
if( temp < aver){
aver = temp;
pos = i;
}
}
System.out.println(numArr[pos+1] + " " + numArr[pos]);
}
}
相关文章推荐
- SQL 根据连续区间 生成数据。 区间拆分
- MySql中group by, order by,按照出现频率排序
- 几种在Linux下查询外网IP的办法。
- 安卓控件使用系列9:Button实现图文混排
- Ubuntu14.04+OpenJDK7下载编译安卓源代码
- [IOS]Xcode7免开发者帐号真机调试运行程序
- MeteoInfoLab脚本示例:获取气团轨迹每个节点的气象数据
- hdu1845 Jimmy’s Assignment --- 完整匹配
- 四种DLL:NON-MFC DLL, Regular DLL Statically/Dynamically Linked to MFC, MFC Extension DLL
- AndroidUI 布局动画-布局内容改变动画
- Number类
- AndroidUI 布局动画-布局内容改变动画
- 面试题6:重建二叉树
- C++容器(五):set类型
- 乱序字符串
- C++容器(五):set类型
- javascript学习笔记
- 20135220谈愈敏--信息安全系统设计基础第三周学习总结
- Binary Tree Paths -- leetcode
- [置顶] OpenLayers 3 之 绘制图形(ol.interaction.Draw)原理解析