赞
踩
有六个人,姓名职业如下:
"小阳", 医生,
"小刚",医生
"小地", 医生, ;
"小温", 律师,
"小红", 律师,
"小丽", 律师,小阳妹妹是小地和小红
小丽哥哥是小刚,小温,
小阳的女朋友是小丽
这六个人里的一个人杀了其余五个人中的一个人。
(1)如果凶手和受害者有亲缘关系,则凶手是位男性;
(2)如果凶手和受害者没有亲缘关系,则凶手是位医生;
(3)如果凶手和受害者职业相同,则受害者是位男性;
(4)如果凶手和受害者没有相同工作,则受害者是位女性;
(5)如果凶手和受害者性别不同,则凶手是位律师;
(6)如果凶手和受害者性别相同,则受害者是位医生。
谁是凶手?
package demo; import java.util.*; public class Lj { static class Person { public String name; public int sex; //性别 1男 2女 public int occupation; //职业 1医生 2律师 public Person() { } public Person(String name, int sex, int occupation) { this.name = name; this.sex = sex; this.occupation = occupation; } //判断是否是亲戚 public boolean isRelative(Person p) { return (relatives1.contains(this.name) && relatives1.contains(p.name)) || (relatives2.contains(this.name) && relatives2.contains(p.name)); } //判断是否相同职业 public boolean isSameOccupation(Person p) { return this.occupation == p.occupation; } //判断性别是否相同 public boolean isSameSex(Person p) { return this.sex == p.sex; } public boolean equals(Object o) { if (o == null) return false; if (o.getClass() == Person.class) { if (((Person) o).name == this.name) { return true; } } return false; } } final static List<Person> PERSON_LIST = new ArrayList<>(); static { PERSON_LIST.add(new Person("小阳", 1, 1)); PERSON_LIST.add(new Person("小刚", 1, 1)); PERSON_LIST.add(new Person("小地", 2, 1)); PERSON_LIST.add(new Person("小温", 1, 2)); PERSON_LIST.add(new Person("小红", 2, 2)); PERSON_LIST.add(new Person("小丽", 2, 2)); } //亲戚1 static Set<String> relatives1 = new HashSet<>(Arrays.asList("小阳,小地,小红".split(","))); //亲戚2 static Set<String> relatives2 = new HashSet<>(Arrays.asList("小丽,小刚,小温".split(","))); public static void main(String[] args) { BREAKPOINT: for (Person murderer : PERSON_LIST) { for (Person victim : PERSON_LIST) { boolean isRelative = murderer.isRelative(victim); boolean isSameOccupation = murderer.isSameOccupation(victim); boolean isSameSex = murderer.isSameSex(victim); boolean f1 = isRelative ? murderer.sex == 1 : murderer.occupation == 1; boolean f2 = isSameOccupation ? victim.sex == 1 : victim.sex == 2; boolean f3 = isSameSex ? victim.occupation == 2 : murderer.occupation == 2; if (f1 && f2 && f3 && !murderer.name.equals(victim.name)) { System.out.println("凶手是" + murderer.name + ",受害人是" + victim.name); break BREAKPOINT; } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。