1、一个字符串,用逗号分隔许多整数,比如: "14,2, 13,4,5,6,7,8,9,11,3"输出为这样的字符窜(连续的数字用’-’表示),比如 "2-9,11,13-14";
2、有一个坛子(Set<Ball> crock),里面装有很多重量颜色不同的球(class Ball {private String color; private int weight; } ) ,一个人想要从坛子里面拿到某种颜色某个重量的球Set<String> holdBalls,一次只能拿出一个球,直到拿到了这个球为止(颜色相同,重量相同或者重量接近且最轻)!
要求实现目的: 1.编程实现寻找球的过程!
2. 编写测试代码证明你的代码是正确的!比如:坛子有三个球,分别是Ball(Black,1KG) ,Ball(Black,21KG) ,Ball(Red,15KG),
如果我需要拿Ball(black,1KG) , 可以找到Ball(black,1KG) ,因为坛子有这个球
如果我需要拿Ball(black,2KG) , 可以找到Ball(black,1KG) ,因为Ball(black,1KG)最接近Ball(black,2KG)
如果我需要拿Ball(black,11KG) , 可以找到Ball(black,1KG) ,因为Ball(black,1KG) 和 Ball(black,21KG)
最接近Ball(black,11KG) ,而且Ball(black,1KG)比Ball(black,21KG)轻
如果我需要拿Ball(black,18KG) , 可以找到Ball(black,21KG) ,因为因为Ball(black,21KG)最接近Ball(black,11KG)
如果我需要拿Ball(Red,2KG) , 可以找到Ball(Red,15KG)
如果我需要拿Ball(Blue,15KG) , 找不到合适的球
代码实例: 以下代码没有上机测试,只是帮助理解题意!可能存在错误,请自行参考更正!
//pojo
public class Ball{
private String color;
private int weight;
//省略get,set method!
}
//具体业务
public class BallManager{
private Set<Ball> crock; // 一个坛子
private Set<String> holdBalls; //想要取得的球
public Map<String,Ball> getBalls(){
// 返回的Map<String,Ball>,keys 为 holdBalls , values 为找到的球
// 请写出具体的代码
}
}
//测试
public class BallManagerTest extends junit.framework.TestCase{
public void testgetBall1(){
//构造坛子,里面的数据我以例子里面的数据
Set<Ball> crock = new HashSet<Ball>();
Ball ball_black1 = new Ball("BLACK",1);crock.add(ball_black1);
Ball ball_black21 = new Ball("BLACK",21);crock.add(ball_black21);
Ball ball_red15 = new Ball("RED",15);crock.add(ball_red15);
//构造想要取得的球
Set<String> holdBalls = new HashSet<String>();
holdBalls.add("black1");
holdBalls.add("black2");
holdBalls.add("blue15");
//调用业务
BallManager ballManager = new BallManager();
ballManager.setCrock(crock);
ballManager.setHoldBalls(holdBalls);
Map<String,Ball> res = ballManager.getBalls();
System.out.println("res.size(): " + res.size());
//测试结果!
//比较Ball("BLACK",1);
Ball b1 = new Ball("BLACK",1); //正确结果,测试者构造
Ball b2 = res.get("black1"); //程序得到结果
System.out.println(check(b1 ,b2)); //为true,表示测试成功 //比较Ball("BLUE",15);
b1 = null; //正确结果,测试者构造
b2 = res.get("blue15"); //程序得到结果
System.out.println(check(b1 ,b2)); //为true,表示测试成功 // 省略,自行补上!
b1 = new Ball("BLACK",21); //正确结果,测试者构造
b2 = res.get("black2"); //程序得到结果
System.out.println(check(b1 ,b2)); //为true,表示测试成功
}
private boolean check(Ball b1 , Ball b2){
if(b1==null && b2==null) {
return true;
}else if(b1==null || b2==null){
return false;
}else{
return b1.getColor().equalsIgnoreCase(b2.getColor()) && b1.getWeight()==b2.getWeight();
}
}
}
2、有一个坛子(Set<Ball> crock),里面装有很多重量颜色不同的球(class Ball {private String color; private int weight; } ) ,一个人想要从坛子里面拿到某种颜色某个重量的球Set<String> holdBalls,一次只能拿出一个球,直到拿到了这个球为止(颜色相同,重量相同或者重量接近且最轻)!
要求实现目的: 1.编程实现寻找球的过程!
2. 编写测试代码证明你的代码是正确的!比如:坛子有三个球,分别是Ball(Black,1KG) ,Ball(Black,21KG) ,Ball(Red,15KG),
如果我需要拿Ball(black,1KG) , 可以找到Ball(black,1KG) ,因为坛子有这个球
如果我需要拿Ball(black,2KG) , 可以找到Ball(black,1KG) ,因为Ball(black,1KG)最接近Ball(black,2KG)
如果我需要拿Ball(black,11KG) , 可以找到Ball(black,1KG) ,因为Ball(black,1KG) 和 Ball(black,21KG)
最接近Ball(black,11KG) ,而且Ball(black,1KG)比Ball(black,21KG)轻
如果我需要拿Ball(black,18KG) , 可以找到Ball(black,21KG) ,因为因为Ball(black,21KG)最接近Ball(black,11KG)
如果我需要拿Ball(Red,2KG) , 可以找到Ball(Red,15KG)
如果我需要拿Ball(Blue,15KG) , 找不到合适的球
代码实例: 以下代码没有上机测试,只是帮助理解题意!可能存在错误,请自行参考更正!
//pojo
public class Ball{
private String color;
private int weight;
//省略get,set method!
}
//具体业务
public class BallManager{
private Set<Ball> crock; // 一个坛子
private Set<String> holdBalls; //想要取得的球
public Map<String,Ball> getBalls(){
// 返回的Map<String,Ball>,keys 为 holdBalls , values 为找到的球
// 请写出具体的代码
}
}
//测试
public class BallManagerTest extends junit.framework.TestCase{
public void testgetBall1(){
//构造坛子,里面的数据我以例子里面的数据
Set<Ball> crock = new HashSet<Ball>();
Ball ball_black1 = new Ball("BLACK",1);crock.add(ball_black1);
Ball ball_black21 = new Ball("BLACK",21);crock.add(ball_black21);
Ball ball_red15 = new Ball("RED",15);crock.add(ball_red15);
//构造想要取得的球
Set<String> holdBalls = new HashSet<String>();
holdBalls.add("black1");
holdBalls.add("black2");
holdBalls.add("blue15");
//调用业务
BallManager ballManager = new BallManager();
ballManager.setCrock(crock);
ballManager.setHoldBalls(holdBalls);
Map<String,Ball> res = ballManager.getBalls();
System.out.println("res.size(): " + res.size());
//测试结果!
//比较Ball("BLACK",1);
Ball b1 = new Ball("BLACK",1); //正确结果,测试者构造
Ball b2 = res.get("black1"); //程序得到结果
System.out.println(check(b1 ,b2)); //为true,表示测试成功 //比较Ball("BLUE",15);
b1 = null; //正确结果,测试者构造
b2 = res.get("blue15"); //程序得到结果
System.out.println(check(b1 ,b2)); //为true,表示测试成功 // 省略,自行补上!
b1 = new Ball("BLACK",21); //正确结果,测试者构造
b2 = res.get("black2"); //程序得到结果
System.out.println(check(b1 ,b2)); //为true,表示测试成功
}
private boolean check(Ball b1 , Ball b2){
if(b1==null && b2==null) {
return true;
}else if(b1==null || b2==null){
return false;
}else{
return b1.getColor().equalsIgnoreCase(b2.getColor()) && b1.getWeight()==b2.getWeight();
}
}
}
private int[] nums = {14,2,13,4,5,6,7,8,9,11,3};
private List<Integer> list; public ddd(){
list = new ArrayList<Integer>();
for(int i=0;i<nums.length;i++){
list.add(nums[i]);
}
}
private void execute(){
Collections.sort(list,new num_comparator());
int temp = list.get(0);
StringBuffer sb = new StringBuffer(temp+"");
for(int i=1;i<list.size();i++){
if(list.get(i)-temp==1){
sb.append("-");
}else{
sb.append(temp+","+list.get(i));
}
temp = list.get(i);
System.out.println(i+":"+sb.toString());
}
sb.append(list.get(list.size()-1));
System.out.println(sb.toString());
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ddd().execute();
}
private class num_comparator implements Comparator<Integer>{ @Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
if(o1.intValue()>o2.intValue()){
return 1;
}else if(o1.intValue()<o2.intValue()){
return -1;
}
return 0;
}
}}
50分送上
b1.getColor().equalsIgnoreCase(b2.getColor()) && b1.getWeight()==b2.getWeight();
String test = "14,2,13,4,5,6,7,8,9,11,3";
List<Integer> list = new ArrayList<Integer>();
for (String str : test.split(",")) {
list.add(Integer.valueOf(str));
}
Collections.sort(list, new Comparator<Integer>() {
public int compare(Integer int1, Integer int2) {
return int1 - int2;
}
}); int i = 0;
Integer[] arr1 = list.toArray(new Integer[list.size()]);
for (; i < arr1.length; i++) {
System.out.print(arr1[i]);
if (i+1 <arr1.length) {
if (arr1[i+1] - (arr1[i]) == 1) {
System.out.print("-");
} else {
System.out.print(",");
}
}
}
}out:
2-3-4-5-6-7-8-9,11,13-14
import java.util.List;public class SerialNumber { /**
* pai xu
* @param str
* @return
*/
public List<Integer> paiXu(String str) {
List<Integer> listInt = new ArrayList<Integer>();
String[] strArray = str.split(",");
try{
for(String s : strArray) {
listInt.add(Integer.parseInt(s));
}
} catch (Exception e) {
e.printStackTrace();
}
for(int i = 0; i < listInt.size() - 1; i++) {
for(int j = i + 1; j < listInt.size(); j++) {
int temp;
if (listInt.get(i) > listInt.get(j)) {
temp = listInt.get(i);
listInt.set(i, listInt.get(j));
listInt.set(j, temp);
}
}
}
return listInt;
}
/**
* Output string
* @param listInt
* @return
*/
public String outString(List<Integer> listInt) {
StringBuilder desStr = new StringBuilder();
boolean bLianxu = false;
if (listInt.size() > 0) {
desStr.append(listInt.get(0));
for(int i = 1; i < listInt.size(); i++) {
if ( (listInt.get(i - 1) + 1 == listInt.get(i)) && !bLianxu) {
desStr.append("-");
bLianxu = true;
}
if (i < listInt.size() - 1) {
if ( (listInt.get(i - 1) + 1 == listInt.get(i))
&& (listInt.get(i) + 1 == listInt.get(i + 1)) ) {
continue;
}
if ( (listInt.get(i - 1) + 1 == listInt.get(i))
&& (listInt.get(i) + 1 != listInt.get(i + 1)) ) {
desStr.append(listInt.get(i));
bLianxu = false;
} else {
desStr.append("," + listInt.get(i));
bLianxu = false;
}
} else {
if (listInt.get(i - 1) + 1 == listInt.get(i)) {
desStr.append(listInt.get(i));
} else {
desStr.append("," + listInt.get(i));
}
}
}
}
return String.valueOf(desStr);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SerialNumber sn = new SerialNumber();
String str = "14,2,13,4,5,6,7,8,9,11,3";
List<Integer> listInt = new ArrayList<Integer>();
listInt = sn.paiXu(str);
System.out.println(listInt);
System.out.println(sn.outString(listInt));
}}输出结果
[2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 14]
2-9,11,13-14