题解:P15521 [CCC 2016 J4] Arrival Time

· · 题解

模拟题……

思路

按照题目意思模拟,一分钟一分钟的看,反正不会超时。

实现

这里建议大家将时间全都转换成一天里的第几分钟,会好处理一些。
同时,建议大家假设一个速度,比如:

代码 WA

::::error[WRONG!!!]

#include<bits/stdc++.h>
using namespace std;
int main(){
    int h, m;
    scanf("%d:%d", &h, &m);
    int t = h * 60 + m;
    int l = 0;
    while (l < 240){
        if ((420 <= t and t <= 600) or (900 <= t and t <= 1140)) l += 1;
        else l += 2;
        h = t / 60, m = t % 60;
        h %= 24;
        t ++;
    }
    h = t / 60, m = t % 60;
    h %= 24;
    printf("%02d:%02d", h, m);
    return 0;
}

然后样例过不去……
在你苦苦调试九九八十一天后,你发现了一个致命问题:有些时候 l 会变成 241,而智慧的程序会让 t++ 再退出循环! :::: 于是,我们充分发扬人类智慧,在程序上打个补丁就溜之大吉^_^。

AC CODE

#include<bits/stdc++.h>
using namespace std;
int main(){
    int h, m;
    scanf("%d:%d", &h, &m); //scanf贴心的帮我们去掉了前缀零^_^
    int t = h * 60 + m;
    int l = 0;
    while (l < 240){
        if ((420 <= t and t <= 600) or (900 <= t and t <= 1140)) l += 1;
        else l += 2;
        h = t / 60, m = t % 60;
        h %= 24;
        t ++;
    }
    if (l > 240) t --; // 世界经典名句:我们充分发扬人类智慧
    h = t / 60, m = t % 60;
    h %= 24;
    printf("%02d:%02d", h, m);//printf贴心的帮我们补上了前缀零^_^
    return 0;
}