飞机场调度

飞机场调度

描述

程序运行开始,首先需要输入以下参数:

机场跑道数,飞机降落占用跑道时间(整数), 飞机起飞占用跑道时间(整数)

整个模拟的时间以分钟为单位,从 0 开始,每分钟的开始需要输入:

该分钟要求降落飞机数, 该分钟要求起飞飞机数

机场调度原则是降落优先起飞,在此原则下按来的顺序排队;每驾飞机都有一个编号,要起飞飞机从 1 开
始,要降落飞机从 5001 开始;每驾飞机需要等待的时间是从其提要求开始到分配跑道为止;每个跑道都
有一个编号(从 1 开始),都可以用来降落和起飞,但同一时间只能被一架飞机占用,占用时间为该飞机
降落(起飞)占用跑道时间。

当输入的要求降落飞机数和要求起飞飞机数都小于 0 时,表示机场关闭,不再接受新的请求,但余下没有
降落(起飞)的飞机需照常进行。

模拟过程中需要随时输出以下数据:

  1. 当前时间 (%4d)

  2. 所有从占用变为空闲的跑道编号 (在输入降落、起飞飞机数前输出)

  3. 可以降落(起飞)飞机编号(% 04d )、跑道编号(% 02d ) (在输入降落、起飞飞机数后输出)
    模拟结束后,程序需输出以下统计结果:

  4. 模拟时间(% 4d )

  5. 降落平均等待时间(% 4.1f )

  6. 起飞平均等待时间(% 4.1f )

  7. 每条跑道被占用时间(% 4d )

  8. 跑道平均被占用的百分比(% 4.1f , 平均占用时间× 100/ 模拟时间)

测试用例及输出(粗体字为输入)

4 3 5
Current Time: 0
1 4
airplane 5001 is ready to land on runway 01
airplane 0001 is ready to takeoff on runway 02
airplane 0002 is ready to takeoff on runway 03
airplane 0003 is ready to takeoff on runway 04
Current Time: 1
0 0
Current Time: 2
0 2
Current Time: 3
runway 01 is free
3 0
airplane 5002 is ready to land on runway 01
Current Time: 4
0 0
Current Time: 5
runway 02 is free
runway 03 is free
runway 04 is free
0 0
airplane 5003 is ready to land on runway 02
airplane 5004 is ready to land on runway 03
airplane 0004 is ready to takeoff on runway 04
Current Time: 6
runway 01 is free
2 4
airplane 5005 is ready to land on runway 01
Current Time: 7
-1 -1
Current Time: 8
runway 02 is free
runway 03 is free
airplane 5006 is ready to land on runway 02
airplane 0005 is ready to takeoff on runway 03
Current Time: 9
runway 01 is free
airplane 0006 is ready to takeoff on runway 01
Current Time: 10
runway 04 is free
airplane 0007 is ready to takeoff on runway 04
Current Time: 11
runway 02 is free
airplane 0008 is ready to takeoff on runway 02
Current Time: 12
Current Time: 13
runway 03 is free
airplane 0009 is ready to takeoff on runway 03
Current Time: 14
runway 01 is free
airplane 0010 is ready to takeoff on runway 01
Current Time: 15
runway 04 is free
Current Time: 16
runway 02 is free
Current Time: 17
Current Time: 18
runway 03 is free
Current Time: 19
runway 01 is free
simulation finished
simulation time: 19
average waiting time of landing: 1.0
average waiting time of takeoff: 4.2
runway 01 busy time: 19
runway 02 busy time: 16
runway 03 busy time: 18
runway 04 busy time: 15
runway average busy time percentage: 89.5%

解题思路

本题的关键在于确定如何表示飞机的起降。这里我们可以定义两个结构,一个表示跑道(用于计算跑道被占用的时间),另一个表示飞机的起降队列。对于给定时刻,首先将起降的飞机加入队列,然后判断跑道是否空闲,如果空闲,则判断起飞队列和降落队列是否为空,如果非空,则第一架飞机出队列。

本题的难点在于队列的建立、添加和删除,跑道状态的判断,以及各部分功能的合并。

具体代码

#include <stdio.h>  
#include <string.h>  
#include<stdlib.h>  
typedef struct  
{  
    int freetime;  
    int totaltime;  
} RunwayType;  
RunwayType runway[105];  
typedef struct _airplane  
{  
    int ID;  
    int time;  
    struct _airplane* next;  
} AirplaneType;  
AirplaneType airplane;  
  
AirplaneType* landhead = NULL, * landrear = NULL;  
void creatlandqueue()  
{  
    AirplaneType* p;  
    p = (AirplaneType*)malloc(sizeof(AirplaneType));  
    p->next = NULL;  
    p->ID = -1;  
    p->time = -1;  
    landhead = p;  
    landrear = p;  
}  
void addlandqueue(int number, int time)  
{  
    AirplaneType* p;  
    p = (AirplaneType*)malloc(sizeof(AirplaneType));  
    p->next = NULL;  
    p->ID = number;  
    p->time = time;  
    landrear->next = p;  
    landrear = landrear->next;  
}  
AirplaneType out_landqueue()  
{  
    AirplaneType* p;  
    p = landhead->next;  
    landhead->next = p->next;  
    if (landhead->next == NULL)  
        landrear = landhead;  
    return *p;  
}  
AirplaneType* offhead = NULL, * offrear = NULL;  
void createoffqueue()  
{  
    AirplaneType* p;  
    p = (AirplaneType*)malloc(sizeof(AirplaneType));  
    p->next = NULL;  
    p->ID = -1;  
    p->time = -1;  
    offhead = p;  
    offrear = p;  
}  
void addoffqueue(int number, int time)  
{  
    AirplaneType* p;  
    p = (AirplaneType*)malloc(sizeof(AirplaneType));  
    p->next = NULL;  
    p->ID = number;  
    p->time = time;  
    offrear->next = p;  
    offrear = offrear->next;  
}  
AirplaneType out_offqueue()  
{  
    AirplaneType* p;  
    p = offhead->next;  
    offhead->next = p->next;  
    if (offhead->next == NULL)  
        offrear = offhead;  
    return *p;  
}  
int isempty(AirplaneType* p)  
{  
    if (p->next == NULL)  
        return 1;  
    else  
        return 0;  
}  
int main()  
{  
    int total_runway, landcost, offcost, currenttime = 0, landnumber, offnumber, flag = 0, landstart = 5001, offstart = 1, i, allfree = 1, land_wait = 0, off_wait = 0, landsum = 0, offsum = 0, totaltime = 0;  
    scanf("%d%d%d", &total_runway, &landcost, &offcost);  
    creatlandqueue();  
    createoffqueue();  
    printf("Current Time: %4d\n", currenttime++);  
    while (1)  
    {  
        if (!flag)  
        {  
            scanf("%d%d", &landnumber, &offnumber);  
            if (landnumber < 0 && offnumber < 0)  
                flag = 1;  
        }  
        for (i = 0; i < landnumber; i++)  
        {  
            addlandqueue(landstart++, currenttime);  
            landsum++;  
        }  
        for (i = 0; i < offnumber; i++)  
        {  
            addoffqueue(offstart++, currenttime);  
            offsum++;  
        }  
        for (i = 1; i <= total_runway; i++)  
        {  
            if (runway[i].freetime == 0)  
            {  
                if (!isempty(landhead))  
                {  
                    airplane = out_landqueue();  
                    runway[i].freetime = landcost;  
                    runway[i].totaltime += landcost;  
                    printf("airplane %04d is ready to land on runway %02d\n", airplane.ID, i);  
                    land_wait += currenttime - airplane.time;  
                }  
                else if (!isempty(offhead))  
                {  
                    airplane = out_offqueue();  
                    runway[i].freetime = offcost;  
                    runway[i].totaltime += offcost;  
                    printf("airplane %04d is ready to takeoff on runway %02d\n", airplane.ID, i);  
                    off_wait += currenttime - airplane.time;  
                }  
            }
        }
        printf("Current Time: %4d\n", currenttime);  
        allfree = 1;  
        for (i = 1; i <= total_runway; i++)  
        {  
            if (runway[i].freetime)  
            {  
                if (!(--runway[i].freetime))  
                    printf("runway %02d is free\n", i);  
                else  
                    allfree = 0;  
            }  
        }  
        if (flag && isempty(landhead) && isempty(offhead) && allfree)  
            break;  
        currenttime++;  
    }  
    printf("simulation finished\nsimulation time: %4d\n", currenttime);  
    double land_average = (double)land_wait / landsum;  
    double off_average = (double)off_wait / offsum;  
    printf("average waiting time of landing: %4.1f\naverage waiting time of takeoff: %4.1f\n", land_average, off_average);  
    for (i = 1; i <= total_runway; i++)  
    {  
        printf("runway %02d busy time: %4d\n", i, runway[i].totaltime);  
        totaltime += runway[i].totaltime;  
    }  
    double percent = ((double)totaltime) / total_runway * 100 / currenttime;  
    printf("runway average busy time percentage: %4.1f%%\n", percent);  
    return 0;  
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值