把起止两个时刻都换算成自 11 月 1 日 00:00 起的绝对分钟数再相减,差值为负输出 -1,全程 O(1)。
OJ: roj
题目 ID: 8011
难度:入门
标签:输入输出数学python
创建: 2026-10-02 16:59
更新: 2026-10-06 16:51
题目描述
毛毛参加一场比赛,比赛从 11 月 11 日上午 11 点 11 分开始。给定结束时刻——11 月第
样例输入:12 13 14,样例输出:1563。
思路
把任意时刻
参考代码
Python 版:
python
#!/usr/bin/env python3
# Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
# rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
# rainboy的学习导航网站: https://idx.roj.ac.cn
# create_at: 2026-10-02 16:59
# update_at: 2026-10-02 16:59
import sys
DAY_MIN = 1440 # 一天的分钟数
def to_minute(day: int, hour: int, minute: int) -> int:
"""把 11 月的 (日, 时, 分) 换算成自 11 月 1 日 00:00 起的绝对分钟数。"""
return (day - 1) * DAY_MIN + hour * 60 + minute
def solve() -> None:
day, hour, minute = map(int, sys.stdin.read().split())
# 起点固定:11 月 11 日 11:11;同一时间轴上相减即为经过的分钟数
spent = to_minute(day, hour, minute) - to_minute(11, 11, 11)
print(spent if spent >= 0 else -1) # 结束早于开始(跨不到起点)时输出 -1
if __name__ == "__main__":
solve()C++ 版(同一算法):
cpp
/**
* Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
* rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
* rainboy的学习导航网站: https://idx.roj.ac.cn
* create_at: 2026-10-06 16:51
* update_at: 2026-10-06 16:51
*/
// 比赛从 11 月 11 日 11:11 开始,把起止时刻都换算成绝对分钟数后相减。
#include <cstdio>
typedef long long ll;
ll day, hour, minute; // 结束时刻:11 月第 day 天的 hour 时 minute 分
int main() {
scanf("%lld %lld %lld", &day, &hour, &minute);
// to_minute(D,H,M) = 自 11 月 1 日 00:00 起的绝对分钟数
ll end_min = (day - 1) * 1440 + hour * 60 + minute;
ll start_min = 10 * 1440 + 11 * 60 + 11; // 起点:11 月 11 日 11:11
ll spent = end_min - start_min;
if (spent < 0) // 结束早于开始
printf("-1\n");
else
printf("%lld\n", spent);
return 0;
}