列表

详情


AB25. ranko的手表

描述

ranko 的手表坏了,正常应该显示 xx:xx 的形式(4 个数字),比如下午 1 点半应该显示 13:30 ,但现在经常会有一些数字有概率无法显示。
ranko 在  时刻看了下时间,过了一段时间在 时刻看了下时间。她想知道, 这两个时刻之间相距的时间的最大值和最小值是多少?
保证 之前(且 不等)。t_2在同一天的 00:00 到 23:59 之间。

输入描述

两行输入两个时间,为 xx:xx 的形式。其中 为数字或者字符 '?' ,问号代表这个数字没有显示。
保证输入是合法的。

输出描述

一行输出两个整数,分别代表 相距时间的最小值和最大值(单位分钟)。

示例1

输入:

18:0?
2?:1?

输出:

121 319

说明:

相距最小的时间为 18:09 到 20:10 ,相距121分钟。
相距最大的时间为 18:00 到 23:19 ,相距319分钟。

原站题解

C 解法, 执行用时: 3ms, 内存消耗: 384KB, 提交时间: 2022-04-05

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<math.h>

int main(){
    char t1[6] = {0};
    char t2[6] = {0};
    for(int i =0; i<5;i++)scanf("%c",&t1[i]);
    getchar();//\n
    for(int i =0; i<5;i++)scanf("%c",&t2[i]);
    
    int* time1 = (int*)calloc(24*60,sizeof(int));
    int* time2 = (int*)calloc(24*60,sizeof(int));
    int index1 = 0,index2 = 0;

    for(int t=0;t<24*60;t++){
        int hour = t/60;
        int minute = t%60;
        if((t1[0] == '?' || t1[0]-'0' == hour/10) && (t1[1] == '?' || t1[1]-'0' == hour%10) \
            && (t1[3] == '?' || t1[3]-'0' == minute/10) && (t1[4] == '?' || t1[4]-'0' == minute%10)){
            time1[index1++] = t;
        }
        if((t2[0] == '?' || t2[0]-'0' == hour/10) && (t2[1] == '?' || t2[1]-'0' == hour%10) \
            && (t2[3] == '?' || t2[3]-'0' == minute/10) && (t2[4] == '?' || t2[4]-'0' == minute%10)){
            time2[index2++] = t;
        }
    }
    
    int max = 0,min = 24*60;
    for(int i=0;i<index1;i++){
        for(int j=0;j<index2;j++){
            if(time1[i] < time2[j]){
                if((time2[j] - time1[i]) > max)max = time2[j] - time1[i];
                if((time2[j] - time1[i]) < min)min = time2[j] - time1[i];
            }
        }
    }
    
    printf("%d %d",min,max);
    return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2021-08-27

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

int main()
{
    string s1,s2;
    cin>>s1>>s2;
    vector<int>v1,v2;
    for(int i=0;i<24*60;i++)
    {
        int h=i/60;
        int m=i%60;
        if((h/10==s1[0]-'0'||s1[0]=='?')&&(h%10==s1[1]-'0'||s1[1]=='?')&&(m/10==s1[3]-'0'||s1[3]=='?')&&(m%10==s1[4]-'0'||s1[4]=='?'))
            v1.push_back(i);
        if((h/10==s2[0]-'0'||s2[0]=='?')&&(h%10==s2[1]-'0'||s2[1]=='?')&&(m/10==s2[3]-'0'||s2[3]=='?')&&(m%10==s2[4]-'0'||s2[4]=='?'))
            v2.push_back(i);
    }
    int mi=1e9,ma=0;
    for(int i=0;i<v1.size();i++)
    {
        for(int j=0;j<v2.size();j++)
        {
            if(v1[i]<v2[j])
            {
                mi=min(mi,v2[j]-v1[i]);
                ma=max(ma,v2[j]-v1[i]);
            }
        }
    }
    cout<<mi<<" "<<ma<<endl;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 400KB, 提交时间: 2021-08-05

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s1,s2;
    cin>>s1>>s2;
    vector<int>v1;
    vector<int>v2;
    for(int i=0;i<24*60;i++){
        int h=i/60,m=i%60;
        if((h/10==s1[0]-'0'||s1[0]=='?')&&(h%10==s1[1]-'0'||s1[1]=='?')&&(m/10==s1[3]-'0'||s1[3]=='?')&&(m%10==s1[4]-'0'||s1[4]=='?')){
            v1.push_back(i);
        }
        if((h/10==s2[0]-'0'||s2[0]=='?')&&(h%10==s2[1]-'0'||s2[1]=='?')&&(m/10==s2[3]-'0'||s2[3]=='?')&&(m%10==s2[4]-'0'||s2[4]=='?')){
            v2.push_back(i);
        }
    }
    int minm=1e9,maxm=0;
    for(int i=0;i<v1.size();i++){
        for(int j=0;j<v2.size();j++){
            if(v1[i]<v2[j]){
                maxm=max(maxm,v2[j]-v1[i]);
                minm=min(minm,v2[j]-v1[i]);
            }
        }
    }
    cout<<minm<<" "<<maxm<<endl;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 408KB, 提交时间: 2022-03-24

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

char MaxChar[6] = "2959";

int time_stamp(string a) {
    string h = a.substr(0, 2);
    string m = a.substr(2, 2);
    int hh = min(23, atoi(h.c_str()));
    int mm = min(59, atoi(m.c_str()));
    return hh * 60 + mm;
}

bool is_valid(string a) {
    if (a[0] < '0' || a[0] > '9') return false;
    if (a[1] < '0' || a[1] > '9') return false;
    if (a[2] < '0' || a[2] > '9') return false;
    if (a[3] < '0' || a[3] > '9') return false;
    string h = a.substr(0, 2);
    if (atoi(h.c_str()) > 23) {
        return false;
    }
    string m = a.substr(2, 2);
    if (atoi(m.c_str()) > 59) {
        return false;
    }
    return true;
}

int max_time(string a, string b) {
    std::replace(a.begin(), a.end(), '?', '0');
    if (b[0] == '2') {
        b[1] = '3';
    }
    for (int i = 0; i < 4; i++) {
        if (b[i] == '?') {
            b[i] = MaxChar[i];
        }
    }
    return time_stamp(b) - time_stamp(a);
}

int dfs_min(string a, string b, int deep) {
    if (deep == 4) {
        if (!is_valid(a) || !is_valid(b)) {
            return 999999;
        }
        int ret = time_stamp(b) - time_stamp(a);
        if (ret <= 0) {
            return 999999;
        }
        return ret;
    }
    
    int ans = 999999;
    if (a[deep] == b[deep]) {
        if (a[deep] == '?') {
//             for (int i = 0; i <= 9; i++) {
//                 for (int j = 0; j <= 9; j++) {
//                     a[deep] = '0' + i;
//                     b[deep] = '0' + j;
//                     ans = min(dfs_min(a, b, deep+1), ans);
//                 }
//             }
            
            a[deep] = '0';
            b[deep] = '0';
            ans = min(dfs_min(a, b, deep+1), ans);
            
            a[deep] = '0';
            b[deep] = '1';
            ans = min(dfs_min(a, b, deep+1), ans);
            
            a[deep] = MaxChar[deep];
            b[deep] = '0';
            ans = min(dfs_min(a, b, deep+1), ans);
            return ans;
        }
        return dfs_min(a, b, deep+1);
    }
    
    if (a[deep] == '?') {
        a[deep] = b[deep];
        ans = dfs_min(a, b, deep+1);
        
        a[deep] = b[deep] - 1;
        ans = min(dfs_min(a, b, deep+1), ans);
    } else if (b[deep] == '?') {
        b[deep] = a[deep];
        ans = dfs_min(a, b, deep+1);
        
        b[deep] = a[deep] + 1;
        ans = min(dfs_min(a, b, deep+1), ans);
    }  else {
        return dfs_min(a, b, deep+1);
    }
    return ans;
}

int main() {
    string a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        a = a.substr(0, 2) + a.substr(3, 2);
        b = b.substr(0, 2) + b.substr(3, 2);
        cout << dfs_min(a, b, 0) << " " << max_time(a, b) << endl;
    }
}
// 64 位输出请用 printf("%lld")

C++ 解法, 执行用时: 3ms, 内存消耗: 652KB, 提交时间: 2021-09-11

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

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    vector<int> v1, v2;
    for (int i = 0; i < 60*24; ++i) {
        int h = i/60, m = i%60;
        if ((s1[0]-'0' == h/10 || s1[0] == '?') && (s1[1]-'0' == h%10 || s1[1] == '?') && (s1[3]-'0' == m/10 || s1[3] == '?') && (s1[4]-'0' == m%10 || s1[4] == '?')) {
            v1.emplace_back(i);
        }
        if ((s2[0]-'0' == h/10 || s2[0] == '?') && (s2[1]-'0' == h%10 || s2[1] == '?') && (s2[3]-'0' == m/10 || s2[3] == '?') && (s2[4]-'0' == m%10 || s2[4] == '?')) {
            v2.emplace_back(i);
        }
    }
    int mi = INT32_MAX, ma = 0;
    for (int i = 0; i < v1.size(); ++i) {
        for (int j = 0; j < v2.size(); ++j) {
            if (v1[i] < v2[j]) {
                mi = min(v2[j]-v1[i], mi);
                ma = max(v2[j]-v1[i], ma);
            }
        }
    }
    cout << mi << " " << ma << endl;
    system("pause");
    return 0;
}

上一题