You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨午夜时段火车换乘C程序的时间差计算问题求助

解决方案

问题核心是未处理跨午夜的时间循环逻辑,直接用分钟数相减会得到不符合实际的差值。以下是修正后的代码及关键修改说明:

修正后的完整代码

#include <stdio.h>
#include <stdlib.h>

#define DAY_MINUTES 1440 // 24*60,一天的总分钟数

int CanTransfer(int xin, int xout, int yin, int yout);

int main()
{
    int ain, aout, bin, bout, cin, cout, inh, inm;
    int ans1, ans2;

    // 输入列车A的时间
    printf("Enter the arrival time of train A:\n");
    scanf("%d:%d", &inh, &inm);
    ain = inh * 60 + inm;

    printf("Enter the departure time of the A train:\n");
    scanf("%d:%d", &inh, &inm);
    aout = inh * 60 + inm;

    // 输入列车B的时间
    printf("Enter the arrival time of train B:\n");
    scanf("%d:%d", &inh, &inm);
    bin = inh * 60 + inm;

    printf("Enter the departure time of the B train:\n");
    scanf("%d:%d", &inh, &inm);
    bout = inh * 60 + inm;

    // 输入列车C的时间
    printf("Enter the arrival time of train C:\n");
    scanf("%d:%d", &inh, &inm);
    cin = inh * 60 + inm;

    printf("Enter the departure time of the C train:\n");
    scanf("%d:%d", &inh, &inm);
    cout = inh * 60 + inm;

    // 判断A的换乘可能性
    ans1 = CanTransfer(ain, aout, bin, bout);
    ans2 = CanTransfer(ain, aout, cin, cout);

    if (ans1 && ans2)
        printf("It is possible to change from train A to trains B and C.\n");
    else if (ans1)
        printf("It is possible to change from train A to train B.\n");
    else if (ans2)
        printf("It is possible to change from train A to train C.\n");
    else
        printf("You cannot change from train A.\n");

    // 判断B的换乘可能性
    ans1 = CanTransfer(bin, bout, ain, aout);
    ans2 = CanTransfer(bin, bout, cin, cout);

    if (ans1 && ans2)
        printf("It is possible to change from train B to trains A and C.\n");
    else if (ans1)
        printf("It is possible to change from train B to train A.\n");
    else if (ans2)
        printf("It is possible to change from train B to train C.\n");
    else
        printf("You cannot change from train B.\n");

    // 判断C的换乘可能性
    ans1 = CanTransfer(cin, cout, ain, aout);
    ans2 = CanTransfer(cin, cout, bin, bout);

    if (ans1 && ans2)
        printf("It is possible to change from train C to trains A and B.\n");
    else if (ans1)
        printf("It is possible to change from train C to train A.\n");
    else if (ans2)
        printf("It is possible to change from train C to train B.\n");
    else
        printf("You cannot change from train C.\n");

    return 0;
}

int CanTransfer(int xin, int xout, int yin, int yout)
{
    // 规则3:判断目标列车在当前到达时间是否处于运营状态
    int is_operating = 0;
    if (yin <= yout) {
        // 非跨天运营:当前时间在列车到达与发车之间
        is_operating = (xin >= yin && xin <= yout);
    } else {
        // 跨天运营:当前时间在列车到达后(当日深夜)或发车前(次日凌晨)
        is_operating = (xin >= yin || xin <= yout);
    }
    if (!is_operating) {
        return 0;
    }

    // 规则1:目标列车发车时间比当前到达时间晚至少5分钟
    int dep_diff = (yout - xin + DAY_MINUTES) % DAY_MINUTES;
    if (dep_diff < 5) {
        return 0;
    }

    // 规则2:两列车到达时间差不超过3小时(180分钟)
    int forward_diff = (yin - xin + DAY_MINUTES) % DAY_MINUTES;
    int backward_diff = (xin - yin + DAY_MINUTES) % DAY_MINUTES;
    int min_arr_diff = (forward_diff < backward_diff) ? forward_diff : backward_diff;
    if (min_arr_diff > 180) {
        return 0;
    }

    return 1;
}

关键修改说明

  1. 新增时间循环常量:定义DAY_MINUTES统一处理一天的分钟数,避免硬编码,提升代码可读性。
  2. 重写换乘判断逻辑:
    • 优先判断运营状态:区分跨天/非跨天场景,准确判断当前时间是否处于目标列车的运营时段。
    • 修正时间差计算:用(目标时间 - 当前时间 + DAY_MINUTES) % DAY_MINUTES计算正向时间差,确保跨午夜场景下得到正确的正差值。
    • 处理到达时间间隔:计算到达时间的最小间隔(取正向、反向差值的较小值),避免跨天导致的差值误判。

运行修正后的代码,输入你提供的测试用例,将得到符合预期的输出结果。

内容的提问来源于stack exchange,提问作者Yazan Ghunaim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 02:30:51