我需要在稳定的婚姻类中实现makeMatches方法。 代码需要遵循此算法:
set each person to be free;
while (some man m with a nonempty preference list is free) {
w = first woman on m's list;
if (some man p is engaged to w) {
set p to be free
}
set m and w to be engaged to each other
for (each successor q of m on w's list) {
delete w from q's preference list
delete q from w's preference list
}
}
但是我找不到我的错误。该计划使夫妇依赖于他们的第一选择,但在此之后不会继续和设定夫妻。 你能看一下我的代码并告诉我它有什么问题吗?
Person.java由讲师提供:
import java.util.*;
public class Person {
public static final int NOBODY = -1;
private String name;
private List<Integer> preferences;
private List<Integer> oldPreferences;
private int partner;
public Person(String name) {
this.name = name;
preferences = new ArrayList<Integer>();
oldPreferences = new ArrayList<Integer>();
erasePartner();
}
public void erasePartner() {
partner = NOBODY;
}
public boolean hasPartner() {
return partner != NOBODY;
}
public int getPartner() {
return partner;
}
public void setPartner(int partner) {
this.partner = partner;
}
public String getName() {
return name;
}
public boolean hasChoices() {
return !preferences.isEmpty();
}
public int getFirstChoice() {
return preferences.get(0);
}
public void addChoice(int person) {
preferences.add(person);
oldPreferences.add(person);
}
public List<Integer> getChoices() {
return preferences;
}
public int getPartnerRank() {
return oldPreferences.indexOf(partner) + 1;
}
}
StablaMarriage.java是我需要实现代码的。
import java.io.*;
import java.util.*;
public class StableMarriage {
public static final String LIST_END = "END";
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("What is the input file? ");
String fileName = console.nextLine();
Scanner input = new Scanner(new File(fileName));
System.out.println();
List<Person> men = readHalf(input);
List<Person> women = readHalf(input);
makeMatches(men, women);
writeList(men, women, "Matches for men");
writeList(women, men, "Matches for women");
}
public static Person readPerson(String line) {
int index = line.indexOf(":");
Person result = new Person(line.substring(0, index));
Scanner data = new Scanner(line.substring(index + 1));
while (data.hasNextInt()) {
result.addChoice(data.nextInt());
}
return result;
}
public static List<Person> readHalf(Scanner input) {
List<Person> result = new ArrayList<Person>();
String line = input.nextLine();
while (!line.equals(LIST_END)) {
result.add(readPerson(line));
line = input.nextLine();
}
return result;
}
public static void makeMatches(List<Person> list1, List<Person> list2) {
for (Person eachMan : list1){
eachMan.setPartner(-1);
}
for (Person eachWoman : list2){
eachWoman.setPartner(-1);
}
for (Person man : list1){
for (Person woman : list2){
while (man.hasChoices() && !man.hasPartner()){
int choosenWoman = man.getFirstChoice();
for (Person p : list1){
if (p.getPartner()== choosenWoman){
p.setPartner(-1);
}
}
man.setPartner(choosenWoman);
list2.get(choosenWoman).setPartner(list1.indexOf(man));
List manList= man.getChoices();
List womanList = woman.getChoices();
for (int q= womanList.size()-1; q>=0; q--){
if(q>womanList.indexOf(list1.indexOf(man)))
manList.remove(manList.indexOf(man.getFirstChoice()));
womanList.remove(q);
}
}
}
}
}
public static void writeList(List<Person> list1, List<Person> list2,
String title) {
System.out.println(title);
System.out.println("Name Choice Partner");
System.out.println("--------------------------------------");
int sum = 0;
int count = 0;
for (Person p : list1) {
System.out.printf("%-15s", p.getName());
if (!p.hasPartner()) {
System.out.println(" -- nobody");
} else {
int rank = p.getPartnerRank();
sum += rank;
count++;
System.out.printf("%4d %s\n", rank,
list2.get(p.getPartner()).getName());
}
}
System.out.println("Mean choice = " + (double) sum / count);
System.out.println();
}
}