中興は月を捧げます-デジタル化の婚姻のペアリング

54783 ワード


  :

      ,             。

      ,           ,       、  、  ,        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。

        ,          ,   ,                    、  、  。

        ,           100 ,       。

               :                   ,          ,  ( )         。

  G11(80、50、40、10、30、60) M(50、60、80、40、10、50)     :

(10*50+30*60+60*80)= 7100 

    MM   GG      :

(40*80+10*50+50*40) = 5700 

  ,      ,         :

1、100   ,  ,   0  99              ,          。

2、             ,         ,        。

3、         ,     ,   99       。

4、       ,            。


Personクラス
public class Person {
    private Integer  id; //  
    private Integer  appearance; //  
    private Integer  character;  //  
    private Integer  wealth; //  
    private Integer  expectAppearance; //    
    private Integer  expectCharacter;  //    
    private Integer  expectWealth; //    
    private Integer  sex; //  

    public Person(Integer id, Integer appearance, Integer character, Integer wealth, Integer expectAppearance, Integer expectCharacter, Integer expectWealth, Integer sex) {
        this.id = id;
        this.appearance = appearance;
        this.character = character;
        this.wealth = wealth;
        this.expectAppearance = expectAppearance;
        this.expectCharacter = expectCharacter;
        this.expectWealth = expectWealth;
        this.sex = sex;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAppearance() {
        return appearance;
    }

    public void setAppearance(Integer appearance) {
        this.appearance = appearance;
    }

    public Integer getCharacter() {
        return character;
    }

    public void setCharacter(Integer character) {
        this.character = character;
    }

    public Integer getWealth() {
        return wealth;
    }

    public void setWealth(Integer wealth) {
        this.wealth = wealth;
    }

    public Integer getExpectAppearance() {
        return expectAppearance;
    }

    public void setExpectAppearance(Integer expectAppearance) {
        this.expectAppearance = expectAppearance;
    }

    public Integer getExpectCharacter() {
        return expectCharacter;
    }

    public void setExpectCharacter(Integer expectCharacter) {
        this.expectCharacter = expectCharacter;
    }

    public Integer getExpectWealth() {
        return expectWealth;
    }

    public void setExpectWealth(Integer expectWealth) {
        this.expectWealth = expectWealth;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

IOテキストファイルの読み込み
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class IOFile {

    public static ArrayList<Person> read(String fileName, Integer sex) throws IOException {
        //    
        String filePath = "D:\\Download\\attachment\\"+fileName;
        ArrayList<Person> peopleArr = new ArrayList<Person>();
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        String str = "";
        String[] split = null;
        //     players  
        if(sex == null){
            while ((str = br.readLine()) != null) {
                split = str.split(",");
                peopleArr.add(new Person(-1,
                        Integer.parseInt(split[1]),
                        Integer.parseInt(split[2]),
                        Integer.parseInt(split[3]),
                        Integer.parseInt(split[4]),
                        Integer.parseInt(split[5]),
                        Integer.parseInt(split[6]),
                        Integer.parseInt(split[0])));
            }
        }else {
            while ((str = br.readLine()) != null) {
                split = str.split(",");
                peopleArr.add(new Person(Integer.parseInt(split[0]),
                        Integer.parseInt(split[1]),
                        Integer.parseInt(split[2]),
                        Integer.parseInt(split[3]),
                        Integer.parseInt(split[4]),
                        Integer.parseInt(split[5]),
                        Integer.parseInt(split[6]),
                        sex));
            }
        }
        return peopleArr;
    }
}


Matchクラス
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Match {
    //     Map  
    static HashMap<Person,Person> maleFemale = new HashMap<Person,Person>();
    static HashMap<Person,Person> FemaleMale = new HashMap<Person,Person>();
    //    Map  
    static HashMap<Person,ArrayList<Person>> femaleMap;
    //    
    public static void rule(ArrayList<Person> femaleArr, ArrayList<Person> maleArr, Person players){
        //    
        if(players.getSex() == 0)
            femaleArr.add(players);
        else
            maleArr.add(players);
        //            
        for(int i = 0;i < 100;i++) {
            //     (    )
            femaleMap = Match.select(femaleArr, maleArr);
            //         
            Person popularFemale = Match.Statistics(femaleMap);
            //                    
            Person frontBoy = Match.personSelect(popularFemale, femaleMap.get(popularFemale));
            //  
            femaleArr.remove(popularFemale);
            maleArr.remove(frontBoy);
            //         
            maleFemale.put(popularFemale, frontBoy);
            FemaleMale.put(frontBoy, popularFemale);
        }
        //          players  
        if(maleFemale.containsKey(players)){
            System.out.println(players.getId()+" : "+maleFemale.get(players).getId());
        }else if(FemaleMale.containsKey(players)){
            System.out.println(FemaleMale.get(players).getId()+" : "+players.getId());
        }else{
            System.out.println("   ");
        }
    }
    
    //    
    public static HashMap<Person,ArrayList<Person>> select(ArrayList<Person> femaleArr, ArrayList<Person> maleArr){
        //    Map  
        HashMap<Person,ArrayList<Person>> femaleMap = new HashMap<Person,ArrayList<Person>>();
        for(int i = 0;i < maleArr.size();i++){
            Person male = maleArr.get(i);
            //         male     
            Person frontGirl = Match.personSelect(male,femaleArr);  
            //  femaleMap key           ,    put
            if(femaleMap.containsKey(frontGirl)){   
            	//  value
                ArrayList<Person> maleArrVal  = femaleMap.get(frontGirl); 
                maleArrVal.add(male);
                femaleMap.put(frontGirl,maleArrVal);
            }else{
            	//  key       put
                ArrayList<Person> maleArrVal = new ArrayList<>(); 
                maleArrVal.add(male);
                femaleMap.put(frontGirl,maleArrVal);
            }
        }
        return femaleMap;
    }

    //    
    public static Person personSelect(Person person,ArrayList<Person> personArr){
    	//     
        int matchValMax = 0;  
        //     
        Person frontPerson = null;  
        for(int i = 0;i < personArr.size();i++){
        	//        
            Person p = personArr.get(i);    
            //   
            int matchValue = Match.Satisfied(person,p);  
            if(matchValue > matchValMax){
                matchValMax = matchValue;
                frontPerson = p;
            }else if(matchValue == matchValMax){    //                       
                if(Match.priority(frontPerson,p))
                    frontPerson = p;
            }
        }
        return frontPerson;
    }

    //         
    public static Person Statistics(HashMap<Person,ArrayList<Person>> femaleMap){
        int sizeMax = 0;
        Person femaleMax = null;
        for (Map.Entry<Person,ArrayList<Person>> entry : femaleMap.entrySet()) {
            Person female = entry.getKey();	
            //        
            int size = entry.getValue().size(); 
            if(size > sizeMax){
                sizeMax = size;
                femaleMax = female;
            }else if(size == sizeMax){
                if(Match.priority(femaleMax,female))
                    femaleMax = female;
            }
        }
        return femaleMax;
    }

    //   
    public static int Satisfied(Person person1,Person person2){
        return  person1.getExpectAppearance() * person2.getAppearance() +
                person1.getExpectCharacter() *  person2.getCharacter() +
                person1.getExpectWealth() *  person2.getWealth();
    }

    //   
    public static boolean priority(Person person1,Person person2){
        int attribute = person1.getAppearance() + person1.getCharacter() + person1.getWealth();
        int attribute2 = person2.getAppearance() + person2.getCharacter() + person2.getWealth();
        //          
        if(attribute < attribute2)
            return true;
        //       id   
        if(attribute == attribute2)
            return person1.getId() > person2.getId();
        return false;
    }

    public static void main(String[] args) throws IOException {
        //   
        String femaleFile = "female.txt";
        String maleFile = "male.txt";
        String playersFile = "players.txt";

        //Person  (0  ,1  ,null   )
        ArrayList<Person> femaleArr = IOFile.read(femaleFile,0);
        ArrayList<Person> maleArr = IOFile.read(maleFile,1);
        ArrayList<Person> playersArr = IOFile.read(playersFile,null);
		//      
        for(int i = 0;i < playersArr.size();i++){
            Person players =  playersArr.get(i);
            //    
            Match.rule((ArrayList<Person>) femaleArr.clone(),(ArrayList<Person>) maleArr.clone(),players);
        }
    }
}