参考程序:
#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;
}