题解:P8071 [COCI 2009/2010 #7] SPAVANAC

· · 题解

题意简述

给定 24 小时制下的时间 H:M,输出 24 小时制下比该时间早 45 分钟对应的时刻。

解题思路

先直接减去 45 分钟:

M\gets M-45

M<0,说明要向前退 1 小时:

H\gets H-1,M\gets M+60

H<0,说明要向前退 1 天:

H\gets H+24

参考代码

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int h,m;
    cin>>h>>m;
    m-=45;
    if(m<0){h-=1;m+=60;}
    if(h<0)h+=24;
    cout<<h<<' '<<m<<'\n';
    return 0;
}