天梯赛座位分配java
时间: 2023-12-20 14:32:11 浏览: 95
天梯赛练习.zip
以下是一个用Java编写的天梯赛座位分配程序的示例:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SeatAllocation {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 输入参赛学校数量
int schoolCount = Integer.parseInt(br.readLine());
// 输入每所学校的队伍数量
String[] teamCounts = br.readLine().split(" ");
int[] teams = new int[schoolCount];
for (int i = 0; i < schoolCount; i++) {
teams[i] = Integer.parseInt(teamCounts[i]);
}
// 计算总的队伍数量
int totalTeams = 0;
for (int i = 0; i < schoolCount; i++) {
totalTeams += teams[i];
}
// 计算总的座位数量
int totalSeats = totalTeams * 10;
// 分配座位
int seatNumber = 1;
for (int i = 0; i < totalTeams; i++) {
for (int j = 0; j < schoolCount; j++) {
if (teams[j] > 0) {
System.out.print(seatNumber + " ");
seatNumber++;
teams[j]--;
}
}
}
// 如果还有剩余的队伍,隔位就坐
for (int i = 0; i < schoolCount; i++) {
while (teams[i] > 0) {
System.out.print(seatNumber + " ");
seatNumber += 2;
teams[i]--;
}
}
}
}
```
阅读全文