PAT-BASIC1026——程式執行時間
阿新 • • 發佈:2018-12-16
題目描述:
知識點:時分秒轉換
思路:按題述一步步運算即可
兩個注意點:
(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++解題報告: