时间限制:C/C++ 1000MS,其他语言 2000MS
内存限制:C/C++ 128MB,其他语言 256MB
难度:中等
分数:100 OI排行榜得分:14(0.1分数+2难度)
描述
警察在侦破一个案件时,得到了线人给出的可能犯罪时间,形如“HH:MM” 表示的时刻。根据警察和线人的约定,为了隐蔽,该时间是修改过的,解密规则为:利用当前出现过的数字,构造下一个距离当前时间最近的时刻,则该时间为可能的犯罪时间。每个出现数字都可以被无限次使用。
输入描述
形如HH:SS的字符串,表示原始输入
输出描述
形如HH:SS的字符串,表示推理出来的犯罪时间
用例输入 1 **
18:52
用例输出 1 **
18:55
提示
- 可以保证线人给定的字符串一定是合法的。例如,“01:35”和“11:08”是合法的,“1:35”和“11:8”是不合法的
. 最近的时刻有可能在第二天
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
//搞个数组把字符串解析成单个的数字,注意跳过冒号。(题目保证输入合法,不用单独校验长度不够、缺少冒号等情况)
int num[4] = { char(str[0] - '0'),char(str[1] - '0'),char(str[3] - '0'),char(str[4] - '0')};
//把时间转为分钟。
int inTime = (num[0] * 10 + num[1]) * 60 + (num[2] * 10 + num[3]);
int minTimeDiff = 24 * 60 * 60;
int minIndex[4] = { 0 };
//暴力破解,找出所有排列组合
for (int index0 = 0; index0 < sizeof(num) / sizeof(int); index0++) {
//因为最大是 23:59,所以首位大于2则直接排除
if (num[index0] > 2) continue;
for (int index1 = 0; index1 < sizeof(num) / sizeof(int); index1++) {
//当小时 首位是2时,第二位最大是3。
if (num[index0] == 2 && num[index1] > 3) continue;
for (int index2 = 0; index2 < sizeof(num) / sizeof(int); index2++) {
//分钟位只能是0-5,不可能出现6-9
if (num[index2] > 5) continue;
for (int index3 = 0; index3 < sizeof(num) / sizeof(int); index3++) {
int newTime = (num[index0] * 10 + num[index1]) * 60 + (num[index2] * 10 + num[index3]);
if (newTime < inTime) {
//如果比当前时间还要早,则把它算到下一天去。
newTime += 24 * 60 * 60;
}
else if (newTime == inTime) {
continue;
}
//找出最小的时刻。
if (newTime - inTime < minTimeDiff) {
minTimeDiff = newTime - inTime;
minIndex[0] = index0;
minIndex[1] = index1;
minIndex[2] = index2;
minIndex[3] = index3;
}
}
}
}
}
cout << num[minIndex[0]] << num[minIndex[1]] << ":" << num[minIndex[2]] << num[minIndex[3]] << endl;
return 0;
}
# 获取输入
str_input = input()
# 解析字符串为单个数字列表,跳过冒号
num = [int(str_input[0]), int(str_input[1]), int(str_input[3]), int(str_input[4])]
# 把输入时间转为分钟
in_time = (num[0] * 10 + num[1]) * 60 + (num[2] * 10 + num[3])
min_time_diff = 24 * 60 # 初始化为一天的分钟数
min_index = [0, 0, 0, 0]
# 暴力枚举所有排列组合
for index0 in range(4):
if num[index0] > 2:
continue
for index1 in range(4):
if num[index0] == 2 and num[index1] > 3:
continue
for index2 in range(4):
if num[index2] > 5:
continue
for index3 in range(4):
new_time = (num[index0] * 10 + num[index1]) * 60 + (num[index2] * 10 + num[index3])
if new_time < in_time:
new_time += 24 * 60 # 跨天处理
elif new_time == in_time:
continue
if new_time - in_time < min_time_diff:
min_time_diff = new_time - in_time
min_index = [index0, index1, index2, index3]
# 输出结果
print(f"{num[min_index[0]]}{num[min_index[1]]}:{num[min_index[2]]}{num[min_index[3]]}")
package main
import (
"fmt"
)
func main() {
var str string
fmt.Scan(&str)
// 解析字符串为单个数字数组,跳过冒号
num := [4]int{
int(str[0] - '0'),
int(str[1] - '0'),
int(str[3] - '0'),
int(str[4] - '0'),
}
// 把输入时间转为分钟
inTime := (num[0]*10 + num[1]) * 60 + (num[2]*10 + num[3])
minTimeDiff := 24 * 60 // 初始化为一天的分钟数
var minIndex [4]int
// 暴力枚举所有排列组合
for index0 := 0; index0 < 4; index0++ {
if num[index0] > 2 {
continue
}
for index1 := 0; index1 < 4; index1++ {
if num[index0] == 2 && num[index1] > 3 {
continue
}
for index2 := 0; index2 < 4; index2++ {
if num[index2] > 5 {
continue
}
for index3 := 0; index3 < 4; index3++ {
newTime := (num[index0]*10 + num[index1]) * 60 + (num[index2]*10 + num[index3])
if newTime < inTime {
newTime += 24 * 60 // 跨天处理
} else if newTime == inTime {
continue
}
if newTime-inTime < minTimeDiff {
minTimeDiff = newTime - inTime
minIndex = [4]int{index0, index1, index2, index3}
}
}
}
}
}
// 输出结果
fmt.Printf("%d%d:%d%d\n", num[minIndex[0]], num[minIndex[1]], num[minIndex[2]], num[minIndex[3]])
}