题解:P5707 【深基2.例12】上学迟到

· · 题解

Solution

给定路程 s 和速度 v,由公式 s=vt 可算出需要走的时间 t。

由于我们要在一个整分钟时刻出发,所以我们计算时间时需要对分钟向上取整。

又因为时间可能会提前到前一天,所以我们首先对 8:00 这个时刻加上一天,然后减去需要的时间即为答案,注意这样我们最后还需要对一天的时间取模,因为可能不需要提前到前一天。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
double s,v,m;
int n,ans1,ans2,t;
double read() {
    int x = 0,f = 1;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x *= 10,x += c - '0';
        c = getchar();
    }
    return x * f;
}
int main() {
    s = read();
    v = read();
    n = 8 * 60 + 24 * 60;
    t = ceil(s / v) + 10;
    n -= t;
    if(n >= 1440) n -= 1440;
    ans1 = n % 60;
    ans2 = n / 60;
    if(ans2 < 10) { 
        if(ans1 < 10) cout << "0" << ans2 << ":0" << ans1;
        else cout << "0" << ans2 << ":" << ans1;
    } 
    else {
        if(ans1 < 10) cout << ans2 << ":0" << ans1;
        else cout << ans2 << ":" << ans1;
    }
    return 0;
}