以下是使用C语言编写的代码实现:
```c
#include <stdio.h>
#include <string.h>
#define MAXN 1005
struct student {
char dorm[10]; // 宿舍楼
char room[5]; // 宿舍号
int breakfast; // 早餐订餐数量
int lunch; // 午餐订餐数量
int dinner; // 晚餐订餐数量
} stu[MAXN];
int n;
int num_song, num_jv, num_liu; // 松园、菊园、柳园的正确订单数
int breakfast_song, breakfast_jv, breakfast_liu; // 早餐数目
int lunch_song, lunch_jv, lunch_liu; // 午餐数目
int dinner_song, dinner_jv, dinner_liu; // 晚餐数目
// 比较函数,用于排序
int cmp(struct student a, struct student b) {
if (strcmp(a.dorm, b.dorm) != 0) {
return strcmp(a.dorm, b.dorm) < 0 ? -1 : 1;
} else if (strcmp(a.room, b.room) != 0) {
return strcmp(a.room, b.room) < 0 ? -1 : 1;
} else {
return 0;
}
}
// 处理一个园区的订单
void process(struct student stu[], int *num, int *breakfast, int *lunch, int *dinner) {
// 将错误的订单分到正确的园区
for (int i = 0; i < n; i++) {
if (strcmp(stu[i].dorm, "song") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
(*num)++;
*breakfast += stu[i].breakfast;
*lunch += stu[i].lunch;
*dinner += stu[i].dinner;
}
} else if (strcmp(stu[i].dorm, "jv") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
(*num)++;
*breakfast += stu[i].breakfast;
*lunch += stu[i].lunch;
*dinner += stu[i].dinner;
}
} else if (strcmp(stu[i].dorm, "liu") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
(*num)++;
*breakfast += stu[i].breakfast;
*lunch += stu[i].lunch;
*dinner += stu[i].dinner;
}
}
}
// 输出该园区的正确订单信息
printf("%d %d %d %d\n", *num, *breakfast, *lunch, *dinner);
for (int i = 0; i < n; i++) {
if (strcmp(stu[i].dorm, "song") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner);
}
} else if (strcmp(stu[i].dorm, "jv") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner);
}
} else if (strcmp(stu[i].dorm, "liu") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner);
}
}
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
// 读入数据
for (int i = 0; i < n; i++) {
scanf("%s %s %d %d %d", stu[i].dorm, stu[i].room, &stu[i].breakfast, &stu[i].lunch, &stu[i].dinner);
}
// 处理松园的订单
num_song = 0;
breakfast_song = 0;
lunch_song = 0;
dinner_song = 0;
process(stu, &num_song, &breakfast_song, &lunch_song, &dinner_song);
// 处理菊园的订单
num_jv = 0;
breakfast_jv = 0;
lunch_jv = 0;
dinner_jv = 0;
process(stu, &num_jv, &breakfast_jv, &lunch_jv, &dinner_jv);
// 处理柳园的订单
num_liu = 0;
breakfast_liu = 0;
lunch_liu = 0;
dinner_liu = 0;
process(stu, &num_liu, &breakfast_liu, &lunch_liu, &dinner_liu);
// 输出结果
printf("song %d %d %d %d\n", num_song, breakfast_song, lunch_song, dinner_song);
qsort(stu, n, sizeof(struct student), cmp);
for (int i = 0; i < n; i++) {
if (strcmp(stu[i].dorm, "song") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner);
}
}
}
printf("jv %d %d %d %d\n", num_jv, breakfast_jv, lunch_jv, dinner_jv);
qsort(stu, n, sizeof(struct student), cmp);
for (int i = 0; i < n; i++) {
if (strcmp(stu[i].dorm, "jv") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner);
}
}
}
printf("liu %d %d %d %d\n", num_liu, breakfast_liu, lunch_liu, dinner_liu);
qsort(stu, n, sizeof(struct student), cmp);
for (int i = 0; i < n; i++) {
if (strcmp(stu[i].dorm, "liu") == 0) {
if (stu[i].room[0] >= '1' && stu[i].room[0] <= '9') {
printf("%s %s %d %d %d\n", stu[i].dorm, stu[i].room, stu[i].breakfast, stu[i].lunch, stu[i].dinner);
}
}
}
}
return 0;
}
```
这里使用了结构体来存储每个同学的信息,方便后续的处理和排序。在处理每个园区的订单时,先将错误的订单分到正确的园区,然后按照楼号和宿舍号的顺序排序并输出。注意在输出之前,要先按照园区的顺序输出该园区的正确订单数目和订餐数目。