poj 1676 What time is it?

本文介绍了一个通过枚举方法解决特定时间匹配问题的算法。该算法旨在对比两个显示不全的时间,其中一个时间比另一个慢15分钟,通过分析并匹配特定的显示模式来确定精确的时间。代码使用C++实现,通过逐位比较数字的显示状态来验证时间的有效性和唯一性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给出两个显示不全的时间,第一个时间是精确的时间,第二个时间慢15分钟,判断是否能推出精确的时间。
分析:直接枚举时间,看能不能匹配到唯一的结果。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const char dig[3][50] = {
" _     _  _     _  _  _  _  _ ",
"| |  | _| _||_||_ |_   ||_||_|",
"|_|  ||_  _|  | _||_|  ||_| _|",
};
char s[3][51];

bool match1(int d, int sj)
{
    for(int i = 0; i < 3; i++)
        for(int j = sj, k = d * 3; j < sj + 3; j++, k++)
            if(s[i][j] != ' ' && dig[i][k] == ' ') return false;
    return true;
}

bool Match(int x, int y)
{
    int getd = 1000;
    if(!match1(x / getd, 0)) return false;
    x %= getd;
    getd /= 10;
    if(!match1(x / getd, 3)) return false;
    x %= getd;
    getd /= 10;
    if(!match1(x / getd, 6)) return false;
    x %= getd;
    getd /= 10;
    if(!match1(x / getd, 9)) return false;

    getd = 1000;
    if(!match1(y / getd, 13)) return false;
    y %= getd;
    getd /= 10;
    if(!match1(y / getd, 16)) return false;
    y %= getd;
    getd /= 10;
    if(!match1(y / getd, 19)) return false;
    y %= getd;
    getd /= 10;
    if(!match1(y / getd, 22)) return false;

    return true;
}

int main()
{
    int t;
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        for(int i = 0; i < 3; i++) gets(s[i]);
        int ans, anscnt = 0;
        for(int i = 0; i <= 2359; i++)
        {
            int m = i % 100;
            int h = i / 100;
            if(h >= 24 || m >= 60) continue;
            m -= 15;
            if(m < 0)
            {
                h -= 1;
                m += 60;
                if(h < 0) h += 24;
            }
            if(h >= 24 || m >= 60) continue;
            int j = h * 100 + m;
            if(Match(i, j))
            {
                anscnt++;
                if(anscnt == 2) break;
                ans = i;
            }
        }
        if(anscnt != 1) puts("Not Sure");
        else printf("%04d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值