博客
关于我
Codeforces B.Planet Lapituletti (模拟时间&镜像)(Round #705 Div.2)
阅读量:161 次
发布时间:2019-02-28

本文共 3139 字,大约阅读时间需要 10 分钟。

镜像时间问题的解决方案

要解决这个问题,我们需要找到从当前时间开始,经过多少分钟后,时间会变成镜像时间。镜像时间是指将时间的每一位数字反转后,得到一个正确的时间。例如,05:11翻转后变成11:20,这是一个有效的时间。

步骤解释:

  • 检查数字是否允许镜像翻转:

    • 每个数字必须是0、1、2、5、8中的一个。这是因为这些数字在翻转后容易形成有效的数字。
  • 镜像翻转数字:

    • 将分钟和小时的每一位数字分别反转。
    • 检查翻转后的分钟和小时是否是有效的时间。
  • 处理输入时间:

    • 逐分钟递增当前时间,直到找到满足镜像条件的时间。
  • 验证镜像时间的有效性:

    • 确保翻转后的时间是有效的,并且比当前时间晚。
  • 代码实现:

    #include 
    #include
    #include
    using namespace std;bool ok(int x) { if (x < 10) { return (x == 0 || x == 1 || x == 2 || x == 5 || x == 8); } else if (x == 100) { return true; } else { int a = x / 10, b = x % 10; bool flag = (a == 0 || a == 1 || a == 2 || a == 5 || a == 8); bool b_flag = (b == 0 || b == 1 || b == 2 || b == 5 || b == 8); return flag && b_flag; }}int fi(int x) { if (x == 0 || x == 1 || x == 8) return x; if (x == 2) return 5; if (x == 5) return 2; return 0; // Should never reach here if input is correct}bool check(int a, int b) { if (!ok(a) || !ok(b)) return false; int x, y; if (b == 100) x = 1; else { int b_digits[2] = {b % 10, b / 10}; x = fi(b_digits[1]) * 10 + fi(b_digits[0]); } if (a == 100) y = 1; else { int a_digits[2] = {a % 10, a / 10}; y = fi(a_digits[1]) * 10 + fi(a_digits[0]); } bool h_ok = (y <= h) && (x >= 0); bool m_ok = (x <= m); return h_ok && m_ok;}int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, h, m, x, y; read(t); while (t--) { read(h, m); int current_h = h, current_m = m; int found = false; for (int i = 0; i <= 1440; ++i) { // 最大24小时后的时间 int current_h_new = current_h, current_m_new = current_m + i; if (current_m_new >= 60) { current_h_new++; current_m_new -= 60; } if (current_h_new > 23) { current_h_new -= 24; current_m_new += 60; } if (current_m_new < 0) { current_h_new--; current_m_new += 60; } if (!ok(current_h_new) || !ok(current_m_new)) continue; if (check(current_m_new, current_h_new)) { found = true; break; } } if (found) { int total = 0; for (int j = 0; j < 1440; ++j) { int new_h = h, new_m = m + j; if (new_m >= 60) { new_h++; new_m -= 60; } if (new_h > 23) { new_h -= 24; new_m += 60; } if (new_m < 0) { new_h--; new_m += 60; } if (!ok(new_h) || !ok(new_m)) continue; if (check(new_m, new_h)) { total = j; break; } } cout << total << endl; } else { cout << -1 << endl; } } return 0;}

    代码解释:

  • ok函数:检查一个数字是否可以镜像翻转。
  • fi函数:反转一个数字。
  • check函数:检查是否存在镜像时间,并返回翻转后的时间。
  • 主函数:读取输入,逐分钟递增当前时间,直到找到镜像时间,输出所需分钟数。
  • 这个解决方案确保了代码的高效性和正确性,能够在合理的时间内处理所有测试用例。

    转载地址:http://vxdj.baihongyu.com/

    你可能感兴趣的文章
    Node-RED中使用range范围节点实现从一个范围对应至另一个范围
    查看>>
    Node-RED中实现HTML表单提交和获取提交的内容
    查看>>
    Node-RED中将CSV数据写入txt文件并从文件中读取解析数据
    查看>>
    Node-RED中建立TCP服务端和客户端
    查看>>
    Node-RED中建立Websocket客户端连接
    查看>>
    Node-RED中建立静态网页和动态网页内容
    查看>>
    Node-RED中解析高德地图天气api的json数据显示天气仪表盘
    查看>>
    Node-RED中连接Mysql数据库并实现增删改查的操作
    查看>>
    Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
    查看>>
    Node-RED中配置周期性执行、指定时间阶段执行、指定时间执行事件
    查看>>
    Node-RED安装图形化节点dashboard实现订阅mqtt主题并在仪表盘中显示温度
    查看>>
    Node-RED怎样导出导入流程为json文件
    查看>>
    Node-RED订阅MQTT主题并调试数据
    查看>>
    Node-RED通过npm安装的方式对应卸载
    查看>>
    node-request模块
    查看>>
    node-static 任意文件读取漏洞复现(CVE-2023-26111)
    查看>>
    Node.js 8 中的 util.promisify的详解
    查看>>
    node.js debug在webstrom工具
    查看>>
    Node.js HTTP模块详解:创建服务器、响应请求与客户端请求
    查看>>
    Node.js RESTful API如何使用?
    查看>>