代码模拟重庆高考平行志愿录取过程

class Candidate {  
    constructor(id, score, rank, volunteers) {  
        this.id = id;  
        this.score = score;  
        this.rank = rank;  
        this.volunteers = volunteers; // Array of { schoolId, majorCode }  
    }  
}  
  
class School {  
    constructor(id, name, capacity, scoreLines) {  
        this.id = id;  
        this.name = name;  
        this.capacity = capacity;  
        this.currentCount = 0;  
        this.scoreLines = scoreLines; // Map of { majorCode: scoreLine }  
    }  
  
    canAdmit(candidateScore, majorCode) {  
        const scoreLine = this.scoreLines[majorCode];  
        if (scoreLine === undefined) {  
            console.error(`No score line found for major code ${majorCode} in school ${this.name}`);  
            return false;  
        }  
        return candidateScore >= scoreLine && this.currentCount < this.capacity;  
    }  
  
    admitStudent() {  
        this.currentCount++;  
    }  
}  
  
function sortCandidates(candidates) {  
    return candidates.sort((a, b) => b.score - a.score || a.rank - b.rank); // Assuming rank breaks ties  
}  
  
function simulateAdmission(candidates, schools) {  
    const sortedCandidates = sortCandidates(candidates);  
  
    for (let candidate of sortedCandidates) {  
        for (let volunteer of candidate.volunteers) {  
            const school = schools.find(s => s.id === volunteer.schoolId);  
            if (school && school.canAdmit(candidate.score, volunteer.majorCode)) {  
                console.log(`Candidate ${candidate.id} with score ${candidate.score} is admitted to ${school.name} for major code ${volunteer.majorCode}`);  
                school.admitStudent();  
                break; // Stop checking other volunteers once admitted  
            }  
        }  
    }  
  
    // Optional: Print schools' current enrollment status  
    for (let school of schools) {  
        console.log(`School ${school.name} has admitted ${school.currentCount} students.`);  
    }  
}  
  
// Example data  
const candidates = [  
    new Candidate(1, 650, 1, [{ schoolId: 1, majorCode: '501' }]),  
    new Candidate(2, 645, 2, [{ schoolId: 2, majorCode: '509' }]),  
    // Add more candidates as needed  
];  
  
const schools = [  
    new School(1, "School A", 100, { '501': 645 }),  
    new School(2, "School B", 150, { '509': 640 }),  
    // Add more schools with corresponding score lines  
];  
  
// Simulate the admission process  
simulateAdmission(candidates, schools);

说明

  1. Candidate 类:志愿列表中的对象现在包含学校ID和专业代码。

  2. School 类:包含一个scoreLines对象,用于存储每个专业代码的录取分数线。canAdmit方法接受考生分数和专业代码作为参数,并根据这些信息判断是否可以录取考生。

  3. simulateAdmission 函数:在检查考生是否符合录取条件时,会传递考生分数和专业代码给canAdmit方法。

  4. 示例数据:考生志愿包含专业代码,学校数据包含对应专业代码的录取分数线。

 

posted @ 2024-06-28 16:33  程序人生♨︎  阅读(18)  评论(0编辑  收藏  举报