具体题目关注我的微博主页 啦啦啦,求关注.
http://weibo.com/2625628052/profile?topnav=1&wvr=5#1347092750648
里面有我转发的李开复的问题..
错了.........
2个人同时关注3个项目的时候出现了一次等待..
顺便求关注:
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.List; public class Main { public static List<Project> allProjects = new ArrayList<Project>(); public static List<Student> allStudents = new ArrayList<Student>(); public static HashMap<String, Project> allProHash = new HashMap<String, Project>(); public static HashMap<String, Student> allStuHash = new HashMap<String, Student>(); public static void main(String[] args) { File input = new File("/home/jushang/iw.in");//测试文件. 随便写... FileInputStream fis; try { fis = new FileInputStream(input); InputStreamReader reader = new InputStreamReader(fis); BufferedReader reader2 = new BufferedReader(reader); String line = reader2.readLine(); while (line != null && line.length() > 0) { //读取所有数据,顺便创建实例 String[] values = line.split(" "); if (values.length != 2) { System.out.println(line); System.out.println("read Date Error!!"); break; } Project p; Student s; if (!allProHash.containsKey(values[1])) { p = new Project(values[1]); allProHash.put(values[1], p); allProjects.add(p); } else { p = allProHash.get(values[1]); } if (allStuHash.containsKey(values[0])) { s = allStuHash.get(values[0]); } else { s = new Student(values[0]); allStuHash.put(values[0], s); allStudents.add(s); } s.addProject(p); p.addStudent(s); line = reader2.readLine(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean isContinue = true; while (isContinue) { //开始按周优先级排序. sort(); for (Student s : allStudents) { s.tick(); } isContinue = false; for (Project p : allProjects) { p.tick(); if (p.isStartMianshi() && p.mianshi()) { //已经开始面试的人优先挑选面试者 isContinue = true; continue; } } for (Project p : allProjects) { if (!p.isStartMianshi() && p.mianshi()) { isContinue = true; } } } sortByName(); for (Project p : allProjects) { p.printResult(); //打印结果 } } public static void sort() { //根据还没有面试人的多少排序,少的排在前面. java.util.Collections.sort(allProjects, new Comparator<Project>() { @Override public int compare(Project object1, Project object2) { // TODO Auto-generated method stub if (object1.getUnInterNumber() > object2.getUnInterNumber()) { return 1; } else if (object1.getUnInterNumber() < object2.getUnInterNumber()) { return -1; } return 0; } }); } public static void sortByName() { java.util.Collections.sort(allProjects, new Comparator<Project>() { @Override public int compare(Project object1, Project object2) { // TODO Auto-generated method stub if (Integer.valueOf(object1.getName()) > Integer.valueOf(object2.getName())) { return 1; } else if (Integer.valueOf(object1.getName()) < Integer.valueOf(object2.getName())) { return -1; } return 0; } }); } }
学生类:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Student { private List<Project> proList = new ArrayList<Project>(); private HashMap<Project, String> proHash = new HashMap<Project, String>(); private List<Project> needInterViewproList = new ArrayList<Project>(); private String name; private int Time = 0; private boolean isStartMianshi = false; private boolean lock = false; private int freeTick = 0; public List<Project> interViewProList = new ArrayList<Project>(); public Student(String name) { this.name = name; } public boolean addProject(Project p) { if (!proHash.containsKey(p)) { proList.add(p); needInterViewproList.add(p); proHash.put(p, p.getName()); return true; } return false; } public boolean mianshi(Project p) { if (proHash.containsKey(p)) { lock = true; freeTick--; needInterViewproList.remove(p); interViewProList.add(p); if (!isStartMianshi) { Time = 1; isStartMianshi = true; } if (needInterViewproList.size() == 0) { isStartMianshi = false; } return true; } return false; } public void tick() { lock = false; if (isStartMianshi) { freeTick++; Time++; } } public boolean isLock() { return lock; } public String getName() { return name; } public boolean isStartMianshi() { return isStartMianshi; } public int getUnviewNumber(){ return needInterViewproList.size(); } }
项目的类:
import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.List; public class Project { private List<Student> StudentList = new ArrayList<Student>(); private HashMap<Student, String> StudentHash = new HashMap<Student, String>(); private List<Student> interedStrudent = new ArrayList<Student>(); private String name; private int Time = 0; private boolean isStartMianshi = false; private String result = ""; public Project(String name) { this.name = name; } public boolean addStudent(Student s) { if (!StudentHash.containsKey(s)) { StudentHash.put(s, s.getName()); StudentList.add(s); } return false; } public boolean mianshi() { sort(); String a = "0 "; for (Student s : StudentList) { if (s.isStartMianshi() && !s.isLock()) { if (!s.mianshi(this)) { System.out.print("error!!!!!!!!" + s.getName()); } else { StudentList.remove(s); interedStrudent.add(s); a = s.getName(); result += a+" "; if (!isStartMianshi) { isStartMianshi = true; Time = 1; } if (StudentList.size() == 0) { isStartMianshi = false; } return true; } } } for(Student s:StudentList){ if (!s.isLock()) { if (!s.mianshi(this)) { System.out.print("error!!!!!!!!" + s.getName()); } else { StudentList.remove(s); interedStrudent.add(s); a = s.getName(); result += a +" "; if (!isStartMianshi) { isStartMianshi = true; Time = 1; } if (StudentList.size() == 0) { isStartMianshi = false; } return true; } } } result += a; if (StudentList.size() == 0) { return false; } else { return true; } } public void tick() { if (isStartMianshi) { Time++; } } public String getName() { return name; } public int getUnInterNumber() { return StudentList.size(); } private void sort() { java.util.Collections.sort(StudentList, new Comparator<Student>() { @Override public int compare(Student object1, Student object2) { // TODO Auto-generated method stub if (object1.getUnviewNumber() > object2.getUnviewNumber()) { return -1; } else if (object1.getUnviewNumber() < object2.getUnviewNumber()) { return 1; } return 0; } }); } public boolean isStartMianshi(){ return isStartMianshi; } public void printResult() { // String s = ""; // for (Student name : interedStrudent) { // s += name.getName() + " "; // } // s += "\n"; System.out.println(name+": "+result); } }
创新工厂竟然要同时发简历过去..., 就贴在这里,顺便求讨论,求正确答案..