题目:
建立一个模型,来模拟推导社会男女择偶过程。为了模型简化,一个人的特性指标有三个,这里假设为财富、样貌、品格,每个指标均可取值1-100之间任意数字。同样也对这3项指标有自己的需求。这3个需求值取值范围都在1-98间,当然三者的和必须为100.所以任意一个人可以用以下数组来表述:G(A、B、C、A1、B1、C1)G代表男,M代表女。举例G11(80、50、40、10、30、60),表示男11号,拥有财富80、样貌50、品格40,对异性品格的偏好为:财富在乎程度百分之10、样貌在乎程度百分之30、品格在乎程度百分之60。同样为了模型简化,假设信息是完全对称的,即是说,每个人都能一眼就能看清楚任意一个人的财富、样貌、品格。package jins.start;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class DengLu extends JFrame {
public JLabel name = new JLabel("用户名");
public JLabel pass = new JLabel("密 码");
public JTextField userName = new JTextField();
public JPasswordField passWord = new JPasswordField();
public Button bok = new Button("登陆");
public Button bexit = new Button("取消");
public DengLu() {
this.setContentPane(new MyPanel());
setTitle("欢迎使用数字配对系统");
setLayout(null);
setSize(500, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation( (scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 - 18);
name.setBounds(70, 260, 120, 20);
userName.setBounds(120, 260, 120, 27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120, 300, 120, 27);
passWord.setEchoChar('*');
bok.setBounds(340, 260, 100, 28);
bexit.setBounds(340, 300, 100, 28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
} else if (passWord.getText().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
} else {
if (userName.getText().equals("admin") &&
passWord.getText().equals("admin")) {
dispose();
Activity activity = new Activity();
activity.init();
Frame frame = new Frame(activity);
frame.show();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("bg.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
package jins.start;
import jins.start.Activity;
import jins.start.Person;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Frame
{
private JFrame frame;
private JTextArea infoArea;
private Activity activity;
public Frame(Activity activity)
{
this.activity = activity;
}
public void show()
{
frame = new JFrame();
frame.setTitle("数字化配对");
frame.setDefaultCloseOperation(3);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
frame.setLocation(width / 2 - 200, height / 2 - 150);
init();
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(800, 400);
}
private void init()
{
InputVerifier verifier = new InputVerifier() {
final Frame this$0;
public boolean verify(JComponent input)
{
JFormattedTextField field = (JFormattedTextField)input;
if (field.isEditValid())
{
if (field.getText() == null || field.getText().trim().equals(""))
{
infoArea.append((new StringBuilder(String.valueOf(field.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return false;
}
int value = Integer.parseInt(field.getText());
if (value >= 1 && value <= 98)
{
return true;
} else
{
infoArea.append((new StringBuilder(String.valueOf(field.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return false;
}
} else
{
infoArea.append((new StringBuilder(String.valueOf(field.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return false;
}
}
{
this$0 = Frame.this;
//super();
}
};
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JTabbedPane westPanel = new JTabbedPane();
JPanel manulPanel = new JPanel();
Box mainBox = Box.createVerticalBox();
Box genderBox = Box.createHorizontalBox();
JLabel genderLabel = new JLabel("性别:");
ButtonGroup group = new ButtonGroup();
final JRadioButton maleButton = new JRadioButton("男", true);
group.add(maleButton);
JRadioButton femaleButton = new JRadioButton("女", false);
group.add(femaleButton);
genderBox.add(genderLabel);
genderBox.add(Box.createHorizontalStrut(10));
genderBox.add(maleButton);
genderBox.add(Box.createHorizontalStrut(10));
genderBox.add(femaleButton);
Box attributeBox = Box.createHorizontalBox();
Box personalBox = Box.createVerticalBox();
JLabel personalLabel = new JLabel("个人属性");
Box fortuneBox = Box.createHorizontalBox();
JLabel fortuneLabel = new JLabel("财富:");
final JFormattedTextField fortuneField = new JFormattedTextField(NumberFormat.getInstance());
fortuneField.setName("个人属性(财富)");
fortuneField.setSize(100, 100);
fortuneField.setInputVerifier(verifier);
fortuneBox.add(fortuneLabel);
fortuneBox.add(Box.createHorizontalStrut(10));
fortuneBox.add(fortuneField);
Box appearanceBox = Box.createHorizontalBox();
JLabel appearanceLabel = new JLabel("外貌:");
final JFormattedTextField appearanceField = new JFormattedTextField(NumberFormat.getInstance());
appearanceField.setName("个人属性(外貌)");
appearanceField.setInputVerifier(verifier);
appearanceBox.add(appearanceLabel);
appearanceBox.add(Box.createHorizontalStrut(10));
appearanceBox.add(appearanceField);
Box charactoreBox = Box.createHorizontalBox();
JLabel charactorLabel = new JLabel("品格:");
final JFormattedTextField charactorField = new JFormattedTextField(NumberFormat.getInstance());
charactorField.setName("个人属性(品格)");
charactorField.setInputVerifier(verifier);
charactoreBox.add(charactorLabel);
charactoreBox.add(Box.createHorizontalStrut(10));
charactoreBox.add(charactorField);
personalBox.add(personalLabel);
personalBox.add(Box.createVerticalStrut(20));
personalBox.add(fortuneBox);
personalBox.add(Box.createVerticalStrut(20));
personalBox.add(appearanceBox);
personalBox.add(Box.createVerticalStrut(20));
personalBox.add(charactoreBox);
Box requireBox = Box.createVerticalBox();
JLabel requireLabel = new JLabel("需求属性");
Box requireFortuneBox = Box.createHorizontalBox();
JLabel requireFortuneLabel = new JLabel("财富:");
final JFormattedTextField requireFortuneField = new JFormattedTextField(NumberFormat.getInstance());
requireFortuneField.setName("需求属性(财富)");
requireFortuneField.setInputVerifier(verifier);
requireFortuneBox.add(requireFortuneLabel);
requireFortuneBox.add(Box.createHorizontalStrut(10));
requireFortuneBox.add(requireFortuneField);
Box requireAppearanceBox = Box.createHorizontalBox();
JLabel requireAppearanceLabel = new JLabel("外貌:");
final JFormattedTextField requireAppearanceField = new JFormattedTextField(NumberFormat.getInstance());
requireAppearanceField.setName("需求属性(外貌)");
requireAppearanceField.setInputVerifier(verifier);
requireAppearanceBox.add(requireAppearanceLabel);
requireAppearanceBox.add(Box.createHorizontalStrut(10));
requireAppearanceBox.add(requireAppearanceField);
Box requireCharactoreBox = Box.createHorizontalBox();
JLabel requireCharactorLabel = new JLabel("品格:");
final JFormattedTextField requireCharactorField = new JFormattedTextField(NumberFormat.getInstance());
requireCharactorField.setName("需求属性(品格)");
requireCharactorField.setInputVerifier(verifier);
requireCharactoreBox.add(requireCharactorLabel);
requireCharactoreBox.add(Box.createHorizontalStrut(10));
requireCharactoreBox.add(requireCharactorField);
requireBox.add(requireLabel);
requireBox.add(Box.createVerticalStrut(20));
requireBox.add(requireFortuneBox);
requireBox.add(Box.createVerticalStrut(20));
requireBox.add(requireAppearanceBox);
requireBox.add(Box.createVerticalStrut(20));
requireBox.add(requireCharactoreBox);
attributeBox.add(personalBox);
attributeBox.add(Box.createHorizontalStrut(40));
attributeBox.add(requireBox);
Box buttonBox = Box.createHorizontalBox();
JButton confirm = new JButton("确定");
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (!fortuneField.isEditValid() || fortuneField.getText().equals(""))
{
infoArea.append((new StringBuilder(String.valueOf(fortuneField.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return;
}
if (!appearanceField.isEditValid() || appearanceField.getText().equals(""))
{
infoArea.append((new StringBuilder(String.valueOf(appearanceField.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return;
}
if (!charactorField.isEditValid() || charactorField.getText().equals(""))
{
infoArea.append((new StringBuilder(String.valueOf(charactorField.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return;
}
int requireFortune = -200;
int requireAppearance = -200;
int requireCharactor = -200;
if (!requireFortuneField.isEditValid() || requireFortuneField.getText().equals(""))
{
infoArea.append((new StringBuilder(String.valueOf(requireFortuneField.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return;
}
requireFortune = Integer.parseInt(requireFortuneField.getText());
if (!requireAppearanceField.isEditValid() || requireAppearanceField.getText().equals(""))
{
infoArea.append((new StringBuilder(String.valueOf(requireAppearanceField.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return;
}
requireAppearance = Integer.parseInt(requireAppearanceField.getText());
if (!requireCharactorField.isEditValid() || requireCharactorField.getText().equals(""))
{
infoArea.append((new StringBuilder(String.valueOf(requireCharactorField.getName()))).append("输入错误,请输入1~98数字 \n").toString());
return;
}
requireCharactor = Integer.parseInt(requireCharactorField.getText());
if (requireFortune + requireAppearance + requireCharactor != 100)
{
infoArea.append("需求属性之和(财富+外貌+品格)必须为100 \n");
} else
{
int gender = maleButton.isSelected() ? 1 : 0;
infoArea.append("产生随机样本... \n");
infoArea.append(activity.generate(gender));
infoArea.append("样本生成完毕 \n");
infoArea.append("匹配结果: \n");
infoArea.append(activity.join(new Person(-1, gender, Integer.parseInt(fortuneField.getText()), Integer.parseInt(appearanceField.getText()), Integer.parseInt(charactorField.getText()), requireFortune, requireAppearance, requireCharactor)));
infoArea.append(" \n");
}
}
});
JButton clear1 = new JButton("清空信息");
clear1.addActionListener(new ActionListener() {
final Frame this$0;
public void actionPerformed(ActionEvent arg0)
{
infoArea.setText("");
}
{
this$0 = Frame.this;
//super();
}
});
buttonBox.add(confirm);
buttonBox.add(Box.createHorizontalStrut(40));
buttonBox.add(clear1);
mainBox.add(genderBox);
mainBox.add(Box.createVerticalStrut(20));
mainBox.add(attributeBox);
mainBox.add(Box.createVerticalStrut(20));
mainBox.add(buttonBox);
manulPanel.add(mainBox);
JScrollPane manulPane = new JScrollPane(mainBox);
westPanel.add("手动输入演示", manulPane);
JPanel autoPanel = new JPanel();
autoPanel.setLayout(new FlowLayout());
JButton start = new JButton("开始");
start.addActionListener(new ActionListener() {
final Frame this$0;
public void actionPerformed(ActionEvent arg0)
{
infoArea.append("100位主角匹配结果如下: \n");
infoArea.append(activity.play());
}
{
this$0 = Frame.this;
//super();
}
});
autoPanel.add(start);
JButton clear2 = new JButton("清空信息");
clear2.addActionListener(new ActionListener() {
final Frame this$0;
public void actionPerformed(ActionEvent arg0)
{
infoArea.setText("");
}
{
this$0 = Frame.this;
//super();
}
});
autoPanel.add(clear2);
westPanel.add("载入样本演示", autoPanel);
infoArea = new JTextArea(20, 35);
infoArea.setLineWrap(true);
infoArea.setEditable(false);
JScrollPane eastPanel = new JScrollPane(infoArea);
mainPanel.add(westPanel, "West");
mainPanel.add(eastPanel, "East");
frame.add(mainPanel);
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("bg.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
载入文件:
package jins.start;
import jins.start.Person;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Loader
{
public Loader()
{
}
public static ArrayList loadPlayerList(String path)
{
ArrayList list;
BufferedReader reader;
list = new ArrayList();
reader = null;
try {
reader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
for (String line = ""; (line = reader.readLine()) != null;)
{
String array[] = line.split(",");
list.add(new Person(-1, Integer.parseInt(array[0]), Integer.parseInt(array[1]), Integer.parseInt(array[2]), Integer.parseInt(array[3]), Integer.parseInt(array[4]), Integer.parseInt(array[5]), Integer.parseInt(array[6])));
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
reader = null;
}
//e;
return list;
}
public static HashMap loadSampleMap(String path, int gender)
{
HashMap map;
BufferedReader reader;
map = new HashMap();
reader = null;
try {
reader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
for (String line = ""; (line = reader.readLine()) != null;)
{
String array[] = line.split(",");
int id = Integer.parseInt(array[0]);
map.put(Integer.valueOf(id), new Person(id, gender, Integer.parseInt(array[1]), Integer.parseInt(array[2]), Integer.parseInt(array[3]), Integer.parseInt(array[4]), Integer.parseInt(array[5]), Integer.parseInt(array[6])));
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
}
模拟投票:
package jins.start;
import java.util.ArrayList;
//Referenced classes of package cn.gucas.dsp.pengf.mg.model:
// Person
public class Vote
{
private int id;
private ArrayList voters;
public Vote()
{
voters = new ArrayList();
}
public void addVoter(Person person)
{
voters.add(person);
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public ArrayList getVoters()
{
return voters;
}
public void setVoters(ArrayList voters)
{
this.voters = voters;
}
}
条件筛选:
package jins.start;
public class Satisfaction implements Comparable
{
private Person person;
private int satisfaction;
public Satisfaction()
{
}
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
public int getSatisfaction()
{
return satisfaction;
}
public void setSatisfaction(int satisfaction)
{
this.satisfaction = satisfaction;
}
public int compareTo(Satisfaction other)
{
if (satisfaction > other.satisfaction)
return -1;
if (satisfaction < other.satisfaction)
return 1;
if (person.getTotalQuality() > other.person.getTotalQuality())
return -1;
if (person.getTotalQuality() < other.person.getTotalQuality())
return 1;
return person.getId() >= other.person.getId() ? 1 : -1;
}
public int compareTo(Object obj)
{
return compareTo((Satisfaction)obj);
}
}
任务属性:
package jins.start;
import java.util.Iterator;
import java.util.PriorityQueue;
// Referenced classes of package cn.gucas.dsp.pengf.mg.model:
// Satisfaction
public class Person
{
private int id;
private int gender;
private int fortune;
private int appearance;
private int character;
private int requireFortune;
private int requireAppearance;
private int requireCharacter;
private PriorityQueue satisfactionQueue;
private PriorityQueue backupSatisfactionQueue;
public Person(int id, int gender, int fortune, int appearance, int character, int requireFortune, int requireAppearance,
int requireCharacter)
{
satisfactionQueue = new PriorityQueue();
backupSatisfactionQueue = new PriorityQueue();
this.id = id;
this.gender = gender;
this.fortune = fortune;
this.appearance = appearance;
this.character = character;
this.requireFortune = requireFortune;
this.requireAppearance = requireAppearance;
this.requireCharacter = requireCharacter;
}
private int getSatisfaction(Person person)
{
return requireFortune * person.fortune + requireAppearance * person.appearance + requireCharacter * person.character;
}
public void judgePerson(Person person)
{
Satisfaction satisfaction = new Satisfaction();
satisfaction.setPerson(person);
satisfaction.setSatisfaction(getSatisfaction(person));
satisfactionQueue.add(satisfaction);
}
public void buildBackupPriorityQueue()
{
backupSatisfactionQueue.clear();
Satisfaction satisfaction;
for (Iterator iterator = satisfactionQueue.iterator(); iterator.hasNext(); backupSatisfactionQueue.add(satisfaction))
satisfaction = (Satisfaction)iterator.next();
}
public void changePlayer(Person person)
{
buildBackupPriorityQueue();
Satisfaction satisfaction = new Satisfaction();
satisfaction.setPerson(person);
satisfaction.setSatisfaction(getSatisfaction(person));
backupSatisfactionQueue.add(satisfaction);
}
public int getTotalQuality()
{
return fortune + appearance + character;
}
public int peekGod()
{
return ((Satisfaction)backupSatisfactionQueue.peek()).getPerson().getId();
}
public int pollGod()
{
return ((Satisfaction)backupSatisfactionQueue.poll()).getPerson().getId();
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getGender()
{
return gender;
}
public void setGender(int gender)
{
this.gender = gender;
}
public int getFortune()
{
return fortune;
}
public void setFortune(int fortune)
{
this.fortune = fortune;
}
public int getAppearance()
{
return appearance;
}
public void setAppearance(int appearance)
{
this.appearance = appearance;
}
public int getCharacter()
{
return character;
}
public void setCharacter(int character)
{
this.character = character;
}
public int getRequireFortune()
{
return requireFortune;
}
public void setRequireFortune(int requireFortune)
{
this.requireFortune = requireFortune;
}
public int getRequireAppearance()
{
return requireAppearance;
}
public void setRequireAppearance(int requireAppearance)
{
this.requireAppearance = requireAppearance;
}
public int getRequireCharacter()
{
return requireCharacter;
}
public void setRequireCharacter(int requireCharacter)
{
this.requireCharacter = requireCharacter;
}
public String toString()
{
return (new StringBuilder("[")).append(id).append(":").append(fortune).append(",").append(appearance).append(",").append(character).append(",").append(requireFortune).append(",").append(requireAppearance).append(",").append(requireCharacter).append("]").toString();
}
}
必要的常量:
package jins.start;
public class Constants
{
public static final String FEMALE_SAMPLE_PATH = "./conf/female.txt";
public static final String MALE_SAMPLE_PATH = "./conf/male.txt";
public static final String PLAYERS_PATH = "./conf/players.txt";
public static final int FEMALE = 0;
public static final int MALE = 1;
public static final int PLAYER_ID = -1;
public static final int N = 100;
public Constants()
{
}
}
下面就是逻辑类了:
package jins.start;
import jins.start.Person;
import jins.start.Vote;
import jins.start.Loader;
import java.util.*;
public class Activity
{
private HashMap femaleMap;
private HashMap maleMap;
private ArrayList playerList;
private HashMap randomFemaleMap;
private HashMap randomMaleMap;
public Activity()
{
}
public void init()
{
femaleMap = Loader.loadSampleMap("./conf/female.txt", 0);
maleMap = Loader.loadSampleMap("./conf/male.txt", 1);
playerList = Loader.loadPlayerList("./conf/players.txt");
}
public String generate(int gender)
{
randomFemaleMap = new HashMap();
randomMaleMap = new HashMap();
StringBuilder sb = new StringBuilder();
sb.append("男生 \t女生 \n");
for (int i = 0; i < 99; i++)
{
int fortune = (int)(Math.random() * 98D + 1.0D);
int appearance = (int)(Math.random() * 98D + 1.0D);
int charactor = (int)(Math.random() * 98D + 1.0D);
int requireFortune = (int)(Math.random() * 98D + 1.0D);
int requireAppearance = (int)(Math.random() * 98D + 1.0D);
int requireCharactor = 100 - requireFortune - requireAppearance;
Person male = new Person(i + 1, 1, fortune, appearance, charactor, requireFortune, requireAppearance, requireCharactor);
randomMaleMap.put(Integer.valueOf(i + 1), male);
sb.append((new StringBuilder()).append(male).append("\t").toString());
fortune = (int)(Math.random() * 98D + 1.0D);
appearance = (int)(Math.random() * 98D + 1.0D);
charactor = (int)(Math.random() * 98D + 1.0D);
requireFortune = (int)(Math.random() * 98D + 1.0D);
requireAppearance = (int)(Math.random() * 98D + 1.0D);
requireCharactor = 100 - requireFortune - requireAppearance;
Person female = new Person(i + 1, 0, fortune, appearance, charactor, requireFortune, requireAppearance, requireCharactor);
randomFemaleMap.put(Integer.valueOf(i + 1), female);
sb.append((new StringBuilder()).append(female).append("\n").toString());
}
if (gender == 1)
{
int fortune = (int)(Math.random() * 98D + 1.0D);
int appearance = (int)(Math.random() * 98D + 1.0D);
int charactor = (int)(Math.random() * 98D + 1.0D);
int requireFortune = (int)(Math.random() * 98D + 1.0D);
int requireAppearance = (int)(Math.random() * 98D + 1.0D);
int requireCharactor = 100 - requireFortune - requireAppearance;
Person female = new Person(100, 0, fortune, appearance, charactor, requireFortune, requireAppearance, requireCharactor);
randomFemaleMap.put(Integer.valueOf(100), female);
sb.append((new StringBuilder(" \t \t")).append(female).append("\n").toString());
} else
{
int fortune = (int)(Math.random() * 98D + 1.0D);
int appearance = (int)(Math.random() * 98D + 1.0D);
int charactor = (int)(Math.random() * 98D + 1.0D);
int requireFortune = (int)(Math.random() * 98D + 1.0D);
int requireAppearance = (int)(Math.random() * 98D + 1.0D);
int requireCharactor = 100 - requireFortune - requireAppearance;
Person male = new Person(100, 1, fortune, appearance, charactor, requireFortune, requireAppearance, requireCharactor);
randomMaleMap.put(Integer.valueOf(100), male);
sb.append((new StringBuilder()).append(male).append("\n").toString());
}
return sb.toString();
}
public String join(Person player)
{
StringBuilder sb = new StringBuilder();
for (Iterator iterator = randomMaleMap.keySet().iterator(); iterator.hasNext();)
{
Integer maleId = (Integer)iterator.next();
Person male = (Person)randomMaleMap.get(maleId);
Person female;
for (Iterator iterator5 = randomFemaleMap.keySet().iterator(); iterator5.hasNext(); female.judgePerson(male))
{
Integer femaleId = (Integer)iterator5.next();
female = (Person)randomFemaleMap.get(femaleId);
male.judgePerson(female);
}
}
HashSet maleGodSet;
HashSet femaleGodSet;
HashMap votes;
if (player.getGender() == 1)
{
randomMaleMap.put(Integer.valueOf(player.getId()), player);
maleGodSet = new HashSet();
femaleGodSet = new HashSet();
Person female;
for (Iterator iterator1 = randomFemaleMap.keySet().iterator(); iterator1.hasNext(); female.changePlayer(player))
{
Integer femaleId = (Integer)iterator1.next();
female = (Person)randomFemaleMap.get(femaleId);
player.judgePerson(female);
}
Person male;
for (Iterator iterator2 = randomMaleMap.keySet().iterator(); iterator2.hasNext(); male.buildBackupPriorityQueue())
{
Integer maleId = (Integer)iterator2.next();
male = (Person)randomMaleMap.get(maleId);
}
votes = new HashMap();
for (Iterator iterator6 = randomMaleMap.keySet().iterator(); iterator6.hasNext();)
{
Integer maleId = (Integer)iterator6.next();
Person male1 = (Person)randomMaleMap.get(maleId);
int femaleGodId = male1.peekGod();
if (votes.containsKey(Integer.valueOf(femaleGodId)))
{
Vote vote = (Vote)votes.get(Integer.valueOf(femaleGodId));
vote.addVoter(male1);
} else
{
Vote vote = new Vote();
vote.setId(femaleGodId);
vote.addVoter(male1);
votes.put(Integer.valueOf(femaleGodId), vote);
}
}
do
{
int maxVote = 0x80000000;
int femaleGodId = 0x80000000;
for (Iterator iterator8 = votes.keySet().iterator(); iterator8.hasNext();)
{
Integer key = (Integer)iterator8.next();
int voteNum = ((Vote)votes.get(key)).getVoters().size();
if (voteNum > maxVote)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (voteNum == maxVote)
{
int competitorQuality = ((Person)randomFemaleMap.get(key)).getTotalQuality();
int femaleGodQuality = ((Person)randomFemaleMap.get(Integer.valueOf(femaleGodId))).getTotalQuality();
if (competitorQuality > femaleGodQuality)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (competitorQuality == femaleGodQuality && key.intValue() < femaleGodId)
{
maxVote = voteNum;
femaleGodId = key.intValue();
}
}
}
femaleGodSet.add(Integer.valueOf(femaleGodId));
Person femaleGod = (Person)randomFemaleMap.get(Integer.valueOf(femaleGodId));
int maleGodId;
for (maleGodId = femaleGod.pollGod(); maleGodSet.contains(Integer.valueOf(maleGodId)) || ((Person)randomMaleMap.get(Integer.valueOf(maleGodId))).peekGod() != femaleGodId; maleGodId = femaleGod.pollGod());
maleGodSet.add(Integer.valueOf(maleGodId));
if (maleGodId == -1)
{
sb.append((new StringBuilder()).append(randomMaleMap.get(Integer.valueOf(maleGodId))).append(" <--> ").append(randomFemaleMap.get(Integer.valueOf(femaleGodId))).append("\n").toString());
return sb.toString();
}
ArrayList voters = ((Vote)votes.get(Integer.valueOf(femaleGodId))).getVoters();
for (Iterator iterator10 = voters.iterator(); iterator10.hasNext();)
{
Person voter = (Person)iterator10.next();
if (voter.getId() != maleGodId)
{
voter.pollGod();
int nextGodId;
for (nextGodId = voter.peekGod(); femaleGodSet.contains(Integer.valueOf(nextGodId)); nextGodId = voter.peekGod())
voter.pollGod();
if (votes.containsKey(Integer.valueOf(nextGodId)))
{
Vote vote = (Vote)votes.get(Integer.valueOf(nextGodId));
vote.addVoter(voter);
} else
{
Vote vote = new Vote();
vote.setId(nextGodId);
vote.addVoter(voter);
votes.put(Integer.valueOf(nextGodId), vote);
}
}
}
votes.remove(Integer.valueOf(femaleGodId));
} while (true);
}
randomFemaleMap.put(Integer.valueOf(player.getId()), player);
maleGodSet = new HashSet();
femaleGodSet = new HashSet();
Person male;
for (Iterator iterator3 = randomMaleMap.keySet().iterator(); iterator3.hasNext(); male.changePlayer(player))
{
Integer maleId = (Integer)iterator3.next();
male = (Person)randomMaleMap.get(maleId);
player.judgePerson(male);
}
Person female;
for (Iterator iterator4 = randomFemaleMap.keySet().iterator(); iterator4.hasNext(); female.buildBackupPriorityQueue())
{
Integer femaleId = (Integer)iterator4.next();
female = (Person)randomFemaleMap.get(femaleId);
}
HashMap femaleId = new HashMap();
for (Iterator iterator7 = randomMaleMap.keySet().iterator(); iterator7.hasNext();)
{
Integer maleId = (Integer)iterator7.next();
Person male1 = (Person)randomMaleMap.get(maleId);
int femaleGodId = male1.peekGod();
if (femaleId.containsKey(Integer.valueOf(femaleGodId)))
{
Vote vote = (Vote)femaleId.get(Integer.valueOf(femaleGodId));
vote.addVoter(male1);
} else
{
Vote vote = new Vote();
vote.setId(femaleGodId);
vote.addVoter(male1);
femaleId.put(Integer.valueOf(femaleGodId), vote);
}
}
do
{
int maxVote = 0x80000000;
int femaleGodId = 0x80000000;
for (Iterator iterator9 = femaleId.keySet().iterator(); iterator9.hasNext();)
{
Integer key = (Integer)iterator9.next();
int voteNum = ((Vote)femaleId.get(key)).getVoters().size();
if (voteNum > maxVote)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (voteNum == maxVote)
{
int competiorQuality = ((Person)randomFemaleMap.get(key)).getTotalQuality();
int femaleGodQuality = ((Person)randomFemaleMap.get(Integer.valueOf(femaleGodId))).getTotalQuality();
if (competiorQuality > femaleGodQuality)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (competiorQuality == femaleGodQuality && key.intValue() < femaleGodId)
{
maxVote = voteNum;
femaleGodId = key.intValue();
}
}
}
femaleGodSet.add(Integer.valueOf(femaleGodId));
Person god = (Person)randomFemaleMap.get(Integer.valueOf(femaleGodId));
int maleGodId;
for (maleGodId = god.pollGod(); maleGodSet.contains(Integer.valueOf(maleGodId)) || ((Person)randomMaleMap.get(Integer.valueOf(maleGodId))).peekGod() != femaleGodId; maleGodId = god.pollGod());
maleGodSet.add(Integer.valueOf(maleGodId));
if (femaleGodId == -1)
{
sb.append((new StringBuilder()).append(randomMaleMap.get(Integer.valueOf(maleGodId))).append(" <--> ").append(randomFemaleMap.get(Integer.valueOf(femaleGodId))).append("\n").toString());
return sb.toString();
}
ArrayList voters = ((Vote)femaleId.get(Integer.valueOf(femaleGodId))).getVoters();
for (Iterator iterator11 = voters.iterator(); iterator11.hasNext();)
{
Person voter = (Person)iterator11.next();
if (voter.getId() != maleGodId)
{
voter.pollGod();
int nextGodId;
for (nextGodId = voter.peekGod(); femaleGodSet.contains(Integer.valueOf(nextGodId)); nextGodId = voter.peekGod())
voter.pollGod();
if (femaleId.containsKey(Integer.valueOf(nextGodId)))
{
Vote vote = (Vote)femaleId.get(Integer.valueOf(nextGodId));
vote.addVoter(voter);
} else
{
Vote vote = new Vote();
vote.setId(nextGodId);
vote.addVoter(voter);
femaleId.put(Integer.valueOf(nextGodId), vote);
}
}
}
femaleId.remove(Integer.valueOf(femaleGodId));
} while (true);
}
public String play()
{
StringBuilder sb = new StringBuilder();
for (Iterator iterator = maleMap.keySet().iterator(); iterator.hasNext();)
{
Integer maleId = (Integer)iterator.next();
Person male = (Person)maleMap.get(maleId);
Person female;
for (Iterator iterator2 = femaleMap.keySet().iterator(); iterator2.hasNext(); female.judgePerson(male))
{
Integer femaleId = (Integer)iterator2.next();
female = (Person)femaleMap.get(femaleId);
male.judgePerson(female);
}
}
int num = 1;
for (Iterator iterator1 = playerList.iterator(); iterator1.hasNext();)
{
Person player = (Person)iterator1.next();
if (player.getGender() == 1)
{
maleMap.put(Integer.valueOf(player.getId()), player);
HashSet maleGodSet = new HashSet();
HashSet femaleGodSet = new HashSet();
Person female;
for (Iterator iterator3 = femaleMap.keySet().iterator(); iterator3.hasNext(); female.changePlayer(player))
{
Integer femaleId = (Integer)iterator3.next();
female = (Person)femaleMap.get(femaleId);
player.judgePerson(female);
}
Person male;
for (Iterator iterator4 = maleMap.keySet().iterator(); iterator4.hasNext(); male.buildBackupPriorityQueue())
{
Integer maleId = (Integer)iterator4.next();
male = (Person)maleMap.get(maleId);
}
HashMap votes = new HashMap();
for (Iterator iterator7 = maleMap.keySet().iterator(); iterator7.hasNext();)
{
Integer maleId = (Integer)iterator7.next();
Person male1 = (Person)maleMap.get(maleId);
int femaleGodId = male1.peekGod();
if (votes.containsKey(Integer.valueOf(femaleGodId)))
{
Vote vote = (Vote)votes.get(Integer.valueOf(femaleGodId));
vote.addVoter(male1);
} else
{
Vote vote = new Vote();
vote.setId(femaleGodId);
vote.addVoter(male1);
votes.put(Integer.valueOf(femaleGodId), vote);
}
}
do
{
int maxVote = 0x80000000;
int femaleGodId = 0x80000000;
for (Iterator iterator9 = votes.keySet().iterator(); iterator9.hasNext();)
{
Integer key = (Integer)iterator9.next();
int voteNum = ((Vote)votes.get(key)).getVoters().size();
if (voteNum > maxVote)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (voteNum == maxVote)
{
int competitorQuality = ((Person)femaleMap.get(key)).getTotalQuality();
int femaleGodQuality = ((Person)femaleMap.get(Integer.valueOf(femaleGodId))).getTotalQuality();
if (competitorQuality > femaleGodQuality)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (competitorQuality == femaleGodQuality && key.intValue() < femaleGodId)
{
maxVote = voteNum;
femaleGodId = key.intValue();
}
}
}
femaleGodSet.add(Integer.valueOf(femaleGodId));
Person femaleGod = (Person)femaleMap.get(Integer.valueOf(femaleGodId));
int maleGodId;
for (maleGodId = femaleGod.pollGod(); maleGodSet.contains(Integer.valueOf(maleGodId)) || ((Person)maleMap.get(Integer.valueOf(maleGodId))).peekGod() != femaleGodId; maleGodId = femaleGod.pollGod());
maleGodSet.add(Integer.valueOf(maleGodId));
if (maleGodId == -1)
{
sb.append((new StringBuilder("[")).append(num++).append("] ").append(maleMap.get(Integer.valueOf(maleGodId))).append(" <--> ").append(femaleMap.get(Integer.valueOf(femaleGodId))).append("\n").toString());
break;
}
if (maleGodSet.size() >= 100)
{
sb.append((new StringBuilder("[")).append(num++).append("] ").append("配对失败").append("\n").toString());
break;
}
ArrayList voters = ((Vote)votes.get(Integer.valueOf(femaleGodId))).getVoters();
for (Iterator iterator11 = voters.iterator(); iterator11.hasNext();)
{
Person voter = (Person)iterator11.next();
if (voter.getId() != maleGodId)
{
voter.pollGod();
int nextGodId;
for (nextGodId = voter.peekGod(); femaleGodSet.contains(Integer.valueOf(nextGodId)); nextGodId = voter.peekGod())
voter.pollGod();
if (votes.containsKey(Integer.valueOf(nextGodId)))
{
Vote vote = (Vote)votes.get(Integer.valueOf(nextGodId));
vote.addVoter(voter);
} else
{
Vote vote = new Vote();
vote.setId(nextGodId);
vote.addVoter(voter);
votes.put(Integer.valueOf(nextGodId), vote);
}
}
}
votes.remove(Integer.valueOf(femaleGodId));
} while (true);
maleMap.remove(Integer.valueOf(player.getId()));
} else
{
femaleMap.put(Integer.valueOf(player.getId()), player);
HashSet maleGodSet = new HashSet();
HashSet femaleGodSet = new HashSet();
Person male;
for (Iterator iterator5 = maleMap.keySet().iterator(); iterator5.hasNext(); male.changePlayer(player))
{
Integer maleId = (Integer)iterator5.next();
male = (Person)maleMap.get(maleId);
player.judgePerson(male);
}
Person female;
for (Iterator iterator6 = femaleMap.keySet().iterator(); iterator6.hasNext(); female.buildBackupPriorityQueue())
{
Integer femaleId = (Integer)iterator6.next();
female = (Person)femaleMap.get(femaleId);
}
HashMap votes = new HashMap();
for (Iterator iterator8 = maleMap.keySet().iterator(); iterator8.hasNext();)
{
Integer maleId = (Integer)iterator8.next();
Person male1 = (Person)maleMap.get(maleId);
int femaleGodId = male1.peekGod();
if (votes.containsKey(Integer.valueOf(femaleGodId)))
{
Vote vote = (Vote)votes.get(Integer.valueOf(femaleGodId));
vote.addVoter(male1);
} else
{
Vote vote = new Vote();
vote.setId(femaleGodId);
vote.addVoter(male1);
votes.put(Integer.valueOf(femaleGodId), vote);
}
}
do
{
int maxVote = 0x80000000;
int femaleGodId = 0x80000000;
for (Iterator iterator10 = votes.keySet().iterator(); iterator10.hasNext();)
{
Integer key = (Integer)iterator10.next();
int voteNum = ((Vote)votes.get(key)).getVoters().size();
if (voteNum > maxVote)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (voteNum == maxVote)
{
int competiorQuality = ((Person)femaleMap.get(key)).getTotalQuality();
int femaleGodQuality = ((Person)femaleMap.get(Integer.valueOf(femaleGodId))).getTotalQuality();
if (competiorQuality > femaleGodQuality)
{
maxVote = voteNum;
femaleGodId = key.intValue();
} else
if (competiorQuality == femaleGodQuality && key.intValue() < femaleGodId)
{
maxVote = voteNum;
femaleGodId = key.intValue();
}
}
}
femaleGodSet.add(Integer.valueOf(femaleGodId));
Person god = (Person)femaleMap.get(Integer.valueOf(femaleGodId));
int maleGodId;
for (maleGodId = god.pollGod(); maleGodSet.contains(Integer.valueOf(maleGodId)) || ((Person)maleMap.get(Integer.valueOf(maleGodId))).peekGod() != femaleGodId; maleGodId = god.pollGod());
maleGodSet.add(Integer.valueOf(maleGodId));
if (femaleGodId == -1)
{
sb.append((new StringBuilder("[")).append(num++).append("] ").append(maleMap.get(Integer.valueOf(maleGodId))).append(" <--> ").append(femaleMap.get(Integer.valueOf(femaleGodId))).append("\n").toString());
break;
}
if (maleGodSet.size() >= 100)
{
sb.append((new StringBuilder("[")).append(num++).append("] ").append("配对失败").append("\n").toString());
break;
}
ArrayList voters = ((Vote)votes.get(Integer.valueOf(femaleGodId))).getVoters();
for (Iterator iterator12 = voters.iterator(); iterator12.hasNext();)
{
Person voter = (Person)iterator12.next();
if (voter.getId() != maleGodId)
{
voter.pollGod();
int nextGodId;
for (nextGodId = voter.peekGod(); femaleGodSet.contains(Integer.valueOf(nextGodId)); nextGodId = voter.peekGod())
voter.pollGod();
if (votes.containsKey(Integer.valueOf(nextGodId)))
{
Vote vote = (Vote)votes.get(Integer.valueOf(nextGodId));
vote.addVoter(voter);
} else
{
Vote vote = new Vote();
vote.setId(nextGodId);
vote.addVoter(voter);
votes.put(Integer.valueOf(nextGodId), vote);
}
}
}
votes.remove(Integer.valueOf(femaleGodId));
} while (true);
femaleMap.remove(Integer.valueOf(player.getId()));
}
}
return sb.toString();
}
}
主类:
package jins.start;
public class MatchGame
{
public MatchGame()
{
}
public static void main(String args[])
{
new DengLu();
}
}