Skip to Content
CoursesCSC122โปรแกรมจำลองการเข้าค่ายเขาชนไก่

โปรแกรมจำลองการเข้าค่ายเขาชนไก่

โจทย์

In a Khao Chon Kai land where only the strongest and most disciplined students are chosen, six elite battalions stand ready to train the next generation of warriors. Each battalion specializes in unique skills and only allows specific student years to enlist. For each year, each student is required to attend the camp for only one batch. Each batch can have only the same year of students although the number of students is less than 660. As a training officer, your task is to determine the latest training day and battalion based on the number of students from each year level and the order of battalion as below.

Each student is assigned to a specific battalion based on their year level:

  • 🐍 Serpent Legion → Only 2nd-year students
  • 🐅 White Tiger Battalion → Only 3rd-year students
  • 🐃 Iron Bull Battalion → Only 4th and 5th-year students
  • 🐆 Shadow Leopard Battalion → Only 2nd and 3rd-year students
  • 🦁 Royal Lion Battalion → Only 4th and 5th-year students
  • 🦅 Eagle King Battalion → Only 2nd and 3rd-year students

Training Camp’s Rules:

  • The training camp lasts for 90 days.
  • Each batch (ผลัด) lasts 3 days.
  • Each battalion (กองพัน) can accommodate 660 students per batch.
  • Each battalion consists of 11 companies (กองร้อย).
  • Each company has 10 platoons (หมวด), and each platoon has 2 squads (หมู่).
  • All battalions start simultaneously.
  • If a battalion allows multiple year levels, the lower year level enrolls first, alternating in each batch.
    • Example: Royal Lion Battalion: Batch 1 → 4th-year, Batch 2 → 5th-year, Batch 3 → 4th-year, and so on.
  • Battalion assignment follows a strict order. If a battalion is full for a batch, the next eligible battalion will be assigned.
    • Example: A 3rd-year student with queue number 661 will be placed in Shadow Leopard instead of White Tiger, as White Tiger has reached its batch limit.

Input:

  • Number of students in each year level (Year 2, Year 3, Year 4, and Year 5)

Output:

  • The latest training day (last day of the last batch), which battalion was assigned the last batch.
  • If all students cannot train within 90 days, return “Some students must wait until next year”.

Explanation of example:

Input:

2000 2000 1950 2000

Output:

Latest Training Day: 12 Last Battalion: Iron Bull Battalion
Battalion \ End day36912
Serpent Legion2nd year - 6602nd year - 202nd year - 02nd year - 0
White Tiger3rd year - 6603rd year - 6603rd year - 03rd year - 0
Iron Bull4th year - 6605th year - 6604th year - 6305th year - 20 (last)
Shadow Leopard2nd year - 6603rd year - 6602nd year - 03rd year - 0
Royal Lion4th year - 6605th year - 6605th year - 6605th year - 0
Eagle King2nd year - 6603rd year - 202nd year - 03rd year - 0
Remaining2nd year = 20
3rd year = 1340
4th year = 630
5th year = 2000
2nd year = 0
3rd year = 0
4th year = 630
5th year = 680
2nd year = 0
3rd year = 0
4th year = 0
5th year = 20
2nd year = 0
3rd year = 0
4th year = 0
5th year = 0
InputOutput
2000
2000
1950
2000
Latest Training Day: 12
Last Battalion: Iron Bull Battalion
2000
2000
2000
2000
Latest Training Day: 12
Last Battalion: Royal Lion Battalion
20000
25000
30000
35000
Some students must wait until next year.

โค้ด

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int year2 = scanner.nextInt(); int year3 = scanner.nextInt(); int year4 = scanner.nextInt(); int year5 = scanner.nextInt(); int batch = 1; String lastBattalion = ""; while (true) { if (isEmpty(year2, year3, year4, year5)) break; if (batch * 3 > 90) { System.out.println("Some students must wait until next year."); return; } // Battalion Serpent Legion (Only Year 2) year2 = remove(year2); if (isEmpty(year2, year3, year4, year5)) { lastBattalion = "Serpent Legion"; break; } // Battalion White Tiger (Only Year 3) year3 = remove(year3); if (isEmpty(year2, year3, year4, year5)) { lastBattalion = "White Tiger"; break; } // Battalion Iron Bull (Year 4 & Year 5) if (year4 > 0 && year5 > 0) { if (batch % 2 == 1) { year4 = remove(year4); } else { year5 = remove(year5); } } else if (year4 > 0) { year4 = remove(year4); } else if (year5 > 0) { year5 = remove(year5); } if (isEmpty(year2, year3, year4, year5)) { lastBattalion = "Iron Bull"; break; } // Battalion Shadow Leopard (Year 2 & Year 3) if (year2 > 0 && year3 > 0) { if (batch % 2 == 1) { year2 = remove(year2); } else { year3 = remove(year3); } } else if (year2 > 0) { year2 = remove(year2); } else if (year3 > 0) { year3 = remove(year3); } if (isEmpty(year2, year3, year4, year5)) { lastBattalion = "Shadow Leopard"; break; } // Battalion Royal Lion (Year 4 & Year 5) if (year4 > 0 && year5 > 0) { if (batch % 2 == 1) { year4 = remove(year4); } else { year5 = remove(year5); } } else if (year4 > 0) { year4 = remove(year4); } else if (year5 > 0) { year5 = remove(year5); } if (isEmpty(year2, year3, year4, year5)) { lastBattalion = "Royal Lion"; break; } // Battalion Eagle King (Year 2 & Year 3) if (year2 > 0 && year3 > 0) { if (batch % 2 == 1) { year2 = remove(year2); } else { year3 = remove(year3); } } else if (year2 > 0) { year2 = remove(year2); } else if (year3 > 0) { year3 = remove(year3); } if (isEmpty(year2, year3, year4, year5)) { lastBattalion = "Eagle King"; break; } batch++; } System.out.println("Latest Training Day: " + batch * 3); System.out.println("Last Battalion: " + lastBattalion + " Battalion"); } public static int remove(int amount) { return Math.max(0, amount - 660); } public static boolean isEmpty(int year2, int year3, int year4, int year5) { return year2 == 0 && year3 == 0 && year4 == 0 && year5 == 0; } }

คำอธิบาย

ปรับปรุงล่าสุด