且画淡云 2021-11-11 20:41 采纳率: 50%
浏览 53
已结题

天梯赛座位题程序出错

PTA上天梯赛的座位题,提交时只过了两个测试点,想问问大家怎么办!
我用过的测试数据有:
(1)一个队伍时的情况, 1 3
(2)3 3 3 2 和 3 2 2 3情况
(3)测试用例
测试时以上数据没有问题,也希望大家能提出其他测试点!谢谢!
原题如下,谢谢!

L1-049 天梯赛座位分配 (20 分)
天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情。为此我们制定如下策略:假设某赛场有 N 所学校参赛,第 i 所学校有 M[i] 支队伍,每队 10 位参赛选手。令每校选手排成一列纵队,第 i+1 队的选手排在第 i 队选手之后。从第 1 所学校开始,各校的第 1 位队员顺次入座,然后是各校的第 2 位队员…… 以此类推。如果最后只剩下 1 所学校的队伍还没有分配座位,则需要安排他们的队员隔位就坐。本题就要求你编写程序,自动为各校生成队员的座位号,从 1 开始编号。

输入格式:
输入在一行中给出参赛的高校数 N (不超过100的正整数);第二行给出 N 个不超过10的正整数,其中第 i 个数对应第 i 所高校的参赛队伍数,数字间以空格分隔。

输出格式:
从第 1 所高校的第 1 支队伍开始,顺次输出队员的座位号。每队占一行,座位号间以 1 个空格分隔,行首尾不得有多余空格。另外,每所高校的第一行按“#X”输出该校的编号X,从 1 开始。
输入样例:
3
3 4 2
结尾无空行
输出样例:
#1
1 4 7 10 13 16 19 22 25 28
31 34 37 40 43 46 49 52 55 58
61 63 65 67 69 71 73 75 77 79
#2
2 5 8 11 14 17 20 23 26 29
32 35 38 41 44 47 50 53 56 59
62 64 66 68 70 72 74 76 78 80
82 84 86 88 90 92 94 96 98 100
#3
3 6 9 12 15 18 21 24 27 30
33 36 39 42 45 48 51 54 57 60
结尾无空行


#include <stdio.h>
#include <stdlib.h>
#define total 10
int N, seat = 1;
int row_position = 0;
struct school{
    int row;
    int *member;
}; 
int minimum (struct school schools[], int complete[]){
    int i = 1;
    int mini = 1;
    int minimum = 10000;
    for (i = 1;i <= N;i ++){
        if (schools[i].row <= minimum){
            if (complete[i] == 0){
            minimum = schools[i].row;
            mini = i;
            }
        }
    }
    return mini;
}
int mark_1 (struct school schools[], int complete[], int school_row[]){
    int min;
    int i;
    min = minimum(schools, complete);
    int j = row_position;
    j = row_position + schools[min].row * total;
    while (row_position < j){
        for (i = 1;i <= N;i ++){
            if (complete[i] == 0){
            schools[i].member[row_position] = seat;
            seat ++;
            }
        }
        row_position ++;
    }
}
void mark_2 (struct school schools[], int complete[], int school_row[]){
    int i = 1;
    for (i = 1;i <= N;i ++){
        if (complete[i] == 0) break;
    }
    while (row_position < school_row[i] * total){
        if (schools[i].member[row_position - 1] == seat - 1){
            seat ++;
        }else{
        schools[i].member[row_position] = seat;
        seat += 2;
        row_position ++;
        }
    }
}
int main (void){
    scanf ("%d", &N);
    int i = 1;
    struct school schools[N+1];
    int school_row[N+1];
    for (i = 1;i <= N;i ++){
        scanf ("%d", &schools[i].row);
        school_row[i] = schools[i].row; 
    }
    for (i = 1;i <= N;i ++){
        schools[i].member = (int *)malloc(sizeof(int) * total * schools[i].row);
    }
    int complete[N+1];
    for (i = 1;i <= N;i ++){
        complete[i] = 0;
    }
    int not_complete = N;
    if (not_complete == 1) {
        mark_2(schools, complete, school_row);
    }else{
        int min;
        while (not_complete > 0){
            if (not_complete == 1){
                mark_2(schools, complete, school_row);
                not_complete --;
            }else{
                min = minimum(schools, complete);
                mark_1(schools, complete, school_row);
                for (i = 1;i <= N;i ++){
                    if (complete[i] == 0){
                        schools[i].row -= schools[min].row;
                        if (schools[i].row == 0){
                            complete[i] = 1;
                            not_complete --;
                        }
                    }
                }
            }
        }
    }
        int count = 0;
        for (i = 1;i <= N;i ++){
            printf ("#%d\n", i);
            for (count = 1;count <= total * school_row[i];count ++){
                printf ("%d", schools[i].member[count - 1]);
                if (count % 10 == 0){
                    printf ("\n");
                }else{
                    printf (" ");
                }
            }
        }
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月12日
  • 已采纳回答 1月4日
  • 创建了问题 11月11日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么