题解:P14497 [NCPC 2025] Crochet Competition

· · 题解

P14497 [NCPC 2025] Crochet Competition 题解

题目传送门

思路

模拟。

可以将每个星期映射到一个数字,方便处理。

然后再将开始和结束时间都转为从周一 00:00 起经过的总分钟数。

如果结束时间小于等于开始时间,说明已经过了一周,要将最终计算的时间加上一周。

最后就是输出了。先忽略值为 0 的单位,然后处理单复数。

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
string d1,d2;
int h1,h2,m1,m2;
char t;
map<string,int> mp {
    {"Mon",0},
    {"Tue",1},
    {"Wed",2},
    {"Thu",3},
    {"Fri",4},
    {"Sat",5},
    {"Sun",6}
};
string print(int x) {
    if (x==0) return "7 days";
    int d=x/(24*60);
    x%=(24*60);
    int h=x/60,m=x%60;
    vector<string> ans;
    if (d>0) ans.push_back(to_string(d)+(d==1 ? " day" : " days"));
    if (h>0) ans.push_back(to_string(h)+(h==1 ? " hour" : " hours"));
    if (m>0) ans.push_back(to_string(m)+(m == 1 ? " minute" : " minutes"));
    if (ans.size()==1) return ans[0];
    if (ans.size()==2) return ans[0]+" and "+ans[1];
    return ans[0]+", "+ans[1]+", "+ans[2];
}
signed main() {
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>d1>>h1>>t>>m1>>d2>>h2>>t>>m2;
    int s=mp[d1]*24*60+h1*60+m1;
    int e=mp[d2]*24*60+h2*60+m2;
    if (e<=s) e+=7*24*60;
    cout<<print(e-s);
    return 0;
}