There are six persons, whose names and occupations are as follows:
"Xiao Yang", doctor,
Xiaogang, doctor
"Small land", doctor;
"Xiao Wen", lawyer,
"Xiaohong", lawyer,
"Xiaoli", lawyer,
Xiaoyang's sister is Xiaodi and Xiaohong
Xiaoli's brother is Xiaogang, Xiaowen,
Xiaoyang's girlfriend is Xiaoli
One of the six killed one of the other five.
(1) If the murderer is related to the victim, the murderer is a male;
(2) if the killer is not related to the victim, the killer is a doctor;
(3) if the murderer and the victim have the same occupation, the victim is a male;
(4) if the murderer and the victim do not have the same job, the victim is a female;
(5) if the sex of the murderer and the victim is different, the murderer is a lawyer;
(6) if the killer and the victim are of the same gender, the victim is a doctor.
Who's the killer?
package demo; import java.util.*; public class Lj { static class Person { public String name; public int sex; //Gender 1, male 2, female public int occupation; //Occupation 1 doctor 2 lawyer public Person() { } public Person(String name, int sex, int occupation) { this.name = name; this.sex = sex; this.occupation = occupation; } //Judge whether they are relatives public boolean isRelative(Person p) { return (relatives1.contains(this.name) && relatives1.contains(p.name)) || (relatives2.contains(this.name) && relatives2.contains(p.name)); } //Judge whether the same occupation public boolean isSameOccupation(Person p) { return this.occupation == p.occupation; } //Judge whether the gender is the same 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("Xiao Yang", 1, 1)); PERSON_LIST.add(new Person("Xiaogang", 1, 1)); PERSON_LIST.add(new Person("Little land", 2, 1)); PERSON_LIST.add(new Person("Xiao Wen", 1, 2)); PERSON_LIST.add(new Person("Xiaohong", 2, 2)); PERSON_LIST.add(new Person("Xiaoli", 2, 2)); } //Kin 1 static Set<String> relatives1 = new HashSet<>(Arrays.asList("Xiao Yang,Little land,Xiaohong".split(","))); //Kin 2 static Set<String> relatives2 = new HashSet<>(Arrays.asList("Xiaoli,Xiaogang,Xiao Wen".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("The killer is" + murderer.name + ",The victim is" + victim.name); break BREAKPOINT; } } } } }