erwugumo
一笑照夜
2019-02-19 01:11

C++段错误的问题,题目为PAT甲级1095

5
  • c++
  • c语言

找不到错了,求教,感觉没有数组越界,没有爆栈,可能是map的问题,刚开始用不太会,debug找不到错很绝望

题目是PAT甲级1095
代码如下

#include<stdio.h>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>

using namespace std;

struct Car
{
    string plate_number;
    int time;
    int InorOut;
    int valid = 0;
}car[10010];

bool cmpcar(Car a, Car b)
{
    int s = a.plate_number.compare(b.plate_number);
    if (s != 0)
        return s < 0;
    else
        return a.time < b.time;
}

bool cmptime(Car a, Car b)
{
    if (a.valid != b.valid)
        return a.valid > b.valid;
    else
        return a.time < b.time;
}

int main()
{
    int N, K;
    scanf("%d %d", &N, &K);
    int i;
    for (i = 0; i < N; i++)
    {
        cin >> car[i].plate_number;
        int hh, mm, ss;
        scanf("%d:%d:%d", &hh, &mm, &ss);
        car[i].time = hh * 3600 + mm * 60 + ss;
        string ioo;
        cin >> ioo;
        if (ioo.compare("in") == 0)
            car[i].InorOut = 1;
        else
            car[i].InorOut = 0;
    }
    car[i].plate_number = "-1";

    sort(car, car + N, cmpcar);
    map<string, int> mp;
    map<string, int>::iterator it;
    int sumtime = 0;
    for (i = 0; i < N; i++)
    {
        if (car[i].plate_number.compare(car[i + 1].plate_number) == 0)
        {
            if (car[i].InorOut == 1 && car[i + 1].InorOut == 0)
            {
                sumtime = sumtime + car[i + 1].time - car[i].time;
                car[i].valid = 1;
                car[i + 1].valid = 1;
            }
        }
        else
        {
            mp[car[i].plate_number] = sumtime;
            sumtime=0;
        }
    }
    /////////////////////////////////////////////////////////////
    sort(car, car + N, cmptime);
    int k = 0;
    int carnum = 0;
    for (i = 0; i < K; i++)
    {
        int H, M, S;
        scanf("%d:%d:%d", &H, &M, &S);
        int Time = H * 3600 + M * 60 + S;
        while (car[k].time <= Time)
        {
            if (car[k].valid == 1)
            {
                if (car[k].InorOut == 1)
                    carnum++;
                else
                    carnum--;
            }
            k++;
        }
        printf("%d\n", carnum);
    }
    ////////////////////////////////////////////////////////////
    int maxnum = 0;
    int max = 0;
    for (it = mp.begin(); it != mp.end(); it++)
    {
        if (it->second > max)
            max = it->second;
    }
    for (it = mp.begin(); it != mp.end(); it++)
    {
        if (it->second == max)
            cout << it->first << " ";
    }
    printf("%02d:%02d:%02d", max / 3600, max % 3600 / 60, max % 60);
}

测试点1和2一直报段错误

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐