x.y.z三个男的。a.b.c三个女的,三对男女参加婚礼,x说是a未来的丈夫,a说他是y的未婚妻,c说他的未来的老公不是x。大家听了以后知道他们都在开玩笑(假话),然后用代码写出真的三对新人?
3条回答 默认 最新
- wanghaolovezlq 2009-08-24 21:31关注
[code="java"]
public class Couple {
private String male;
private String female;
@Override
public String toString() {
// TODO Auto-generated method stub
return "male:" + male + "---female:" + female;
}
public Couple(String male, String female) {
super();
this.male = male;
this.female = female;
}
public String getMale() {
return male;
}
public void setMale(String male) {
this.male = male;
}
public String getFemale() {
return female;
}
public void setFemale(String female) {
this.female = female;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((female == null) ? 0 : female.hashCode());
result = prime * result + ((male == null) ? 0 : male.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Couple other = (Couple) obj;
if (female == null) {
if (other.female != null)
return false;
} else if (!female.equals(other.female))
return false;
if (male == null) {
if (other.male != null)
return false;
} else if (!male.equals(other.male))
return false;
return true;
}}
[/code]
[code="java"]
import java.util.*;
import java.util.Map.Entry;public class Main {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String[] males = {"x","y","z"}; String[] females = {"a","b","c"}; //保存所有可能的夫妻对 Set<Couple> set = new HashSet<Couple>(); for(int i=0;i<males.length;i++) { for(int j=0;j<females.length;j++) { set.add(new Couple(males[i],females[j])); } } //x说是a未来的丈夫(假话) set.remove(new Couple("x","a")); //a说他是y的未婚妻(假话) set.remove(new Couple("y","a")); //c说他的未来的老公不是x(假话) for(int i=0;i<males.length;i++) { for(int j=0;j<females.length;j++) { if(males[i].equals("x") && !females[j].equals("c")) set.remove(new Couple(males[i],females[j])); if(!males[i].equals("x") && females[j].equals("c")) set.remove(new Couple(males[i],females[j])); } } if(set.size() == males.length) { System.out.println("成功得到所有的夫妻对如下:" + set); } else { Map<String, List<Couple>> map = new HashMap<String, List<Couple>>(); Iterator<Couple> it = set.iterator(); Couple c ; String male; while(it.hasNext()) { c = it.next(); male = c.getMale(); if(map.get(male) == null) { map.put(male, new ArrayList<Couple>()); map.get(male).add(c); } else { map.get(male).add(c); } } Iterator<Entry<String, List<Couple>>> itmap = map.entrySet().iterator(); Entry<String, List<Couple>> entry; //真正匹配的夫妻 List<Couple> match = new ArrayList<Couple>(); while(itmap.hasNext()) { entry = itmap.next(); if(entry.getValue().size() == 1) { match.add(entry.getValue().get(0)); } } //待删除的夫妻 List<Couple> waitRemove = new ArrayList<Couple>(); for(Couple cp : match) { for(Couple cSet : set) { if(!cp.equals(cSet) && cSet.getFemale().equals(cp.getFemale())) waitRemove.add(cSet); } } set.removeAll(waitRemove); System.out.println("成功得到所有的夫妻对如下:" + set); } }
}
[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报