GESP2024年6月认证C++一级( 第三部分编程题(1)休息时间)

参考程序:

#include <bits/stdc++.h> // 万能头文件,包含所有常用库
using namespace std;

int main() {
    int h, m, s;
    cin >> h >> m >> s; // 输入小时、分钟、秒
    int k;
    cin >> k;           // 输入学习时长(单位:秒)

    // 将当前时间转换为总秒数
    int now = h * 60 * 60 + m * 60 + s;

    // 加上学习时间 k 秒
    now += k;

    // 重新把秒数拆分为 h:m:s 格式
    int hh = now / 3600;      // 小时数
    now %= 3600;              // 剩余秒数
    int mm = now / 60;        // 分钟数
    now %= 60;                // 剩余的秒数就是 ss

    cout << hh << " " << mm << " " << now << "\n";  // 输出结果
}

参考程序:(超过24小时跨天)

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

int main() {
    int h, m, s;
    cin >> h >> m >> s;   // 输入初始时间(24 小时制)
    int k;
    cin >> k;             // 输入学习秒数

    // 转换为总秒数
    int total = h * 3600 + m * 60 + s;
    total += k;

    // 取模一天的总秒数(防止超出一天)
    total %= 86400; // 86400 = 24 * 3600 秒,一整天

    // 转换为 h:m:s
    int hh = total / 3600;
    total %= 3600;
    int mm = total / 60;
    int ss = total % 60;

    cout << hh << " " << mm << " " << ss << "\n";
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汉克老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值