满江紫求助

P1167 刷题

CXH114 @ 2024-10-23 22:26:31

rt 附代码:

#include <bits/stdc++.h>
using namespace std;
int month[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int pr[5005];
int main()
{
    long long t1 = 0, t2 = 0, sum = 0;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> pr[i];
    }
    sort(pr, pr + n);
    int y1, m1, d1, h1, mi1, y2, m2, d2, h2, mi2;
    string d[2];
    cin >> d[0] >> d[1];
    for (int i = 0; i < 2; i++)
    {
        int in = 0, x = 0, c = 1000;
        vector<int> s;
        while (d[i][in] != '-')
        {
            s.push_back(d[i][in++] - '0');
        }
        for (int i = 0; i < 4; i++)
        {
            x += s[i] * c;
            c /= 10;
        }
        if (i == 0)
            y1 = x;
        else
            y2 = x;
        in++;
        x = 0, c = 10;
        s.clear();
        while (d[i][in] != '-')
        {
            s.push_back(d[i][in++] - '0');
        }
        for (int i = 0; i < 2; i++)
        {
            x += s[i] * c;
            c /= 10;
        }
        if (i == 0)
            m1 = x;
        else
            m2 = x;
        in++;
        x = 0, c = 10;
        s.clear();
        while (d[i][in] != '-')
        {
            s.push_back(d[i][in++] - '0');
        }
        for (int i = 0; i < 2; i++)
        {
            x += s[i] * c;
            c /= 10;
        }
        if (i == 0)
            d1 = x;
        else
            d2 = x;
        in++;
        x = 0, c = 10;
        s.clear();
        while (d[i][in] != ':')
        {
            s.push_back(d[i][in++] - '0');
        }
        for (int i = 0; i < 2; i++)
        {
            x += s[i] * c;
            c /= 10;
        }
        if (i == 0)
            h1 = x;
        else
            h2 = x;
        in++;
        x = 0, c = 10;
        s.clear();
        while (d[i][in] != ':')
        {
            s.push_back(d[i][in++] - '0');
        }
        for (int i = 0; i < 2; i++)
        {
            x += s[i] * c;
            c /= 10;
        }
        if (i == 0)
            mi1 = x;
        else
            mi2 = x;
        in++;
    }
    if ((y1 % 4 == 0 && y1 % 100 != 0) || y1 % 400 == 0)
    {
        month[1]++;
        t1 = y1 * 366 * 24 * 60;
        t1 += month[m1 - 1] * 24 * 60;
        t1 += d1 * 24 * 60;
        t1 += h1 * 60;
        t1 += mi1;
        month[1]--;
    }
    else
    {
        t1 = y1 * 365 * 24 * 60;
        t1 += month[m1 - 1] * 24 * 60;
        t1 += d1 * 24 * 60;
        t1 += h1 * 60;
        t1 += mi1;
    }
    if ((y2 % 4 == 0 && y2 % 100 != 0) || y2 % 400 == 0)
    {
        month[1]++;
        t2 = y2 * 366 * 24 * 60;
        t2 += month[m2 - 1] * 24 * 60;
        t2 += d2 * 24 * 60;
        t2 += h2 * 60;
        t2 += mi2;
        month[1]--;
    }
    else
    {
        t2 = y2 * 365 * 24 * 60;
        t2 += month[m2 - 1] * 24 * 60;
        t2 += d2 * 24 * 60;
        t2 += h2 * 60;
        t2 += mi2;
    }
    long long fint = t2 - t1, index = 0;
    while (fint >= pr[index])
    {
        sum++;
        fint -= pr[index++];
    }
    cout << sum;
    return 0;
}

悬棺


by Peizekai @ 2024-10-23 22:32:20

建议重构:cin>>int可以直接判断下一个是不是char类型('-’)


by An_OIer @ 2024-10-27 22:25:24

@CXH114

    scanf("%4lld-%2lld-%2lld-%2lld:%2lld",year1,month1,day1,hour1,minte1);
    scanf("%4lld-%2lld-%2lld-%2lld:%2lld",year2,month2,day2,hour2,minte2);

可能会好一些


|