程式碼模擬重慶高考平行志願錄取過程

程序人生♨︎發表於2024-06-28
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. 示例資料:考生志願包含專業程式碼,學校資料包含對應專業程式碼的錄取分數線。

相關文章