简单思路:
- 遍历所有的时间,即从0到1439。
- 将时间转换为XX:XX的格式
- 看给定的两个时间是否符合格式,把符合格式的保存起来。
- 遍历所有符合格式时间的最大最小值。
思路简单,但是写起来稍微有点麻烦。
#include <bits/stdc++.h> #include <string> using namespace std; string f(int n) { int m = n % 60; int h = (n - m) / 60; std::stringstream ss; ss << std::setw(2) << std::setfill('0') << m; // 宽度为2,不足补0 string mm = ss.str(); std::stringstream bb; bb << std::setw(2) << std::setfill('0') << h; // 宽度为2,不足补0 string hh = bb.str(); string ans = hh + ":" + mm; return ans; } bool fit(string a, string b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] == '?' || b[i] == '?') continue; if (a[i] != b[i]) return false; } return true; } int main() { string t1, t2; cin >> t1 >> t2; set<int> first, second; for (int i = 0; i < 1440; i++) { string now = f(i); if (fit(now, t1)) { first.insert(i); } if (fit(now, t2)) { second.insert(i); } } int mi = 2000, ma = -2000; for (auto i : second) { for (auto j : first) { if (i - j >= 1) mi = min(mi, i - j); ma = max(ma, i - j); } } cout << mi << ' ' << ma << '\n'; } // 64 位输出请用 printf("%lld")