top1编程
← 返回题目
题解

【入门】时间转换

1 条题解

  • 0
    @ 2026-7-30 0:43:53

    解题思路

    按照题目要求直接计算。代码中使用简单的循环、判断和数组,把每一步写出来,方便理解。

    参考代码

    // 读取题目给出的数据。
    // 按照题意完成计算。
    // 输出最终答案。
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main() {
        int n;
        cin >> n;
        int h = n / 3600;
        int m = n % 3600 / 60;
        int s = n % 60;
        cout << setw(2) << setfill('0') << h << ":";
        cout << setw(2) << setfill('0') << m << ":";
        cout << setw(2) << setfill('0') << s << endl;
        return 0;
    }
    

    复杂度

    代码只使用了题目需要的循环、判断和数组。若有 n 次循环,时间复杂度为 O(n);只使用固定数量变量时,空间复杂度为 O(1)。

    • 1