我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805295203598336
题目描述:
知识点:时分秒转换
思路:按题述一步步运算即可
两个注意点:
(1)不足1秒的时间四舍五入。
(2)个位数需要前面补0。
时间复杂度是O(n / 60),其中n为输入两个时间的时间间隔。空间复杂度是O(1)。
C++代码:
#include<iostream>
#include<time.h>
using namespace std;
int main() {
long startTime;
long endTime;
cin >> startTime >> endTime;
long time = (endTime - startTime) / 100;
if ((endTime - startTime) % 100 >= 50) {
time++;
}
if (time >= 60 * 60) {
int hour = time / (60 * 60);
if (hour <= 9) {
cout << "0" << hour << ":";
} else {
cout << hour << ":";
}
int miniute = (time - hour * 60 * 60) / 60;
if (miniute <= 9) {
cout << "0" << miniute << ":";
} else {
cout << miniute << ":";
}
int second = time - hour * 60 * 60 - miniute * 60;
if (second <= 9) {
cout << "0" << second;
} else {
cout << second;
}
} else if (time >= 60) {
cout << "00:";
int miniute = time / 60;
if (miniute <= 9) {
cout << "0" << miniute << ":";
} else {
cout << miniute << ":";
}
int second = time - miniute * 60;
if (second <= 9) {
cout << "0" << second;
} else {
cout << second;
}
} else {
cout << "00:00:";
int second = time;
if (second <= 9) {
cout << "0" << second;
} else {
cout << second;
}
}
}
C++解题报告: