博客
关于我
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/

    你可能感兴趣的文章
    NMF(非负矩阵分解)
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>