兩個乒乓球隊進行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽籤決定比賽名單
看到一個面試題 寫了玩玩
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class 比賽選手 {
public static void main(String[] args) {
Map map = getGameMatch();
map = getFinalMathcher((HashMap) map,null);
for(Entry entry : map.entrySet()){
System.out.println(entry.getKey()+"和"+entry.getValue()+"是一對選手");
}
}
public static Map getGameMatch(){
String [] abc = {"a","b","c"};
String [] xyz = {"x","y","z"};
Map ht = new HashMap();
int index = 1;
for(String temp1 : abc){
for(String temp2 : xyz){
if("a".equals(temp1)&&"x".equals(temp2)){
continue;
}
if("c".equals(temp1)&&"x".equals(temp2)){
continue;
}
if("c".equals(temp1)&&"z".equals(temp2)){
continue;
}
ht.put(index+"",temp1+"="+temp2);
index++;
}
}
return ht;
}
public static HashMap getFinalMathcher(HashMap ht,List lists){
List names = new ArrayList();
List values = new ArrayList();
for(Entry e : ht.entrySet()){
names.add( e.getValue().split("=")[0] );
values.add( e.getValue().split("=")[1] );
}
if(isRep(names) && isRep(values)){
ht = new HashMap();
for(int i=0;i
ht.put(names.get(i), values.get(i));
}
return ht;
}
HashMap> m = (HashMap) getIndex(names,lists);
int index = 0;
lists = new ArrayList();
if(m.size()>0){
for(Entry e : m.entrySet()){
index = Integer.parseInt(e.getKey()+"");
lists.addAll((List) e.getValue());
}
}
String value = values.get(index);
for(int i=0;i
if((i!=index) && value.equals(values.get(i))){
names.remove(i);
values.remove(i);
}
}
ht = new HashMap();
for(int i=0;i
ht.put((i+1)+"" , names.get(i)+"="+values.get(i));
}
return getFinalMathcher(ht,lists);
}
public synchronized static HashMap getIndex(List strs,List chain){
if(chain==null){
chain = new ArrayList();
}
int index = 0;
if(strs.size()<=1){
Map hm = new HashMap>();
hm.put(index, chain);
return (HashMap) hm;
}
boolean flag ;
for(int i=0;i
flag = true;
for(int j=0;j
if(j==i){
continue;
}
if(strs.get(i).equals(strs.get(j))){
flag = false;
break;
}
}