求助,只会c语言,VS运行没问题,全是wa
查看原帖
求助,只会c语言,VS运行没问题,全是wa
842630
DJ__yyy楼主2023/9/10 22:33
#include<stdio.h>
#include<stdlib.h>

typedef struct node {
    int time;
    int price;
    struct node* next;
}QueueNode,*QueueNodePtr;

typedef struct queue {
    QueueNode* front;//队头指针
    QueueNode* rear;//队尾指针
}Queue,*QueuePtr;

int main()
{
    //第一行输入整数n,代表乘车记录的数量
    int n = 0;
    scanf("%d",&n);
    //接下来n行,每行三个整数
    int w;//0和1分别代表地铁和公交车
    int p;//乘车票价
    int t;//乘车时间

    int z = 0;//总花费
    QueuePtr Q = (QueuePtr)malloc(sizeof(Queue));
    QueueNode hNode;
    Q->front = &hNode;
    Q->rear = &hNode;
    Q->rear->next = NULL;//初始化为空队
    Q->front = Q->rear;

    for (int i = 0; i < n; i++) {
        scanf("%d %d %d",&w,&p,&t);
        //如果乘坐地铁,则总花费增加,并增加一张优惠票,时效45分钟
        if (!w) {
            z = z + p;//总花费增加
            QueueNodePtr dc = (QueueNodePtr)malloc(sizeof(QueueNode));
            dc->time = t;//记录增加优惠票此刻的时间,即乘坐地铁的时刻
            dc->price = p;//记录该时刻的地铁票价
            dc->next = NULL;
            Q->rear->next = dc;
            Q->rear = dc;//实现优惠票队列的插入操作
        }
        //如果乘坐公交车,则先判断是否有优惠票
        else {
            QueueNodePtr dc = (QueueNodePtr)malloc(sizeof(QueueNode));
            dc = Q->front->next;
            while (dc) {
                //逐一判断优惠票是否过期,如果过期,则出队,往后判断
                if (t - dc->time > 45) {
                    
                    QueueNodePtr px = (QueueNodePtr)malloc(sizeof(QueueNode));
                    px = Q->front->next;
                    Q->front->next = Q->front->next->next;
                    dc = Q->front->next;
                    free(px);
                }
                //如果未过期,则比较两次票价
                else {
                    if (p <= dc->price) {//本次公交车票价不超过地铁票价,使用该优惠票(单独出队),循环结束
                        QueueNodePtr px = (QueueNodePtr)malloc(sizeof(QueueNode));
                        px = Q->front->next;
                        if (px != dc) {//dc结点在队列中间
                            while (px->next != dc) {
                                px = px->next;
                            }
                            px->next = dc->next;//删除dc结点
                        }
                        else {//dc结点在队列头部
                            Q->front->next = Q->front->next->next;
                        }
                        free(dc);
                        p = 0;
                        break;
                    }
                    else {//本次公交车票价超过地铁票价,dc往后移继续判断
                        dc = dc->next;
                    }
                }
            }
            if (Q->front->next == NULL) Q->rear->next = NULL;
            //如果优惠票队列遍历完,没有优惠票可使用,则p不变,不做任何改动
            z = z + p;
        }
        //printf("%d\n",z);
    }

    printf("%d",z);
    return 0;
}
2023/9/10 22:33
加载中...