Kodewick
開始學 →
小挑戰←→0 / 86

廚房小鐘

#045小挑戰minute >= 60約 7 分鐘

廚房裡有個 24 小時制的小鐘 Clock。寫 add(minutes):往後撥 minutes 分鐘(0 或更多)。分鐘滿 60 進一小時,小時到 24 就從 0 開始。show() 已經寫好,顯示成 09:15。

示例
clock_after(8, 30, 45)→'09:15'
clock_after(23, 50, 20)→'00:10'
函式string clock_after(int h, int m, int minutes)
main.cpp
#include <string>
using namespace std;

string two(int x) {
    string s = to_string(x);
    if (x < 10) {
        s = "0" + s;
    }
    return s;
}

class Clock {
public:
    int hour;
    int minute;

    Clock(int h, int m) {
        hour = h;
        minute = m;
    }

    void add(int minutes) {
        // 在這裡寫:往後撥 minutes 分鐘
    }

    string show() const {
        return two(hour) + ":" + two(minute);
    }
};

string clock_after(int h, int m, int minutes) {
    Clock c(h, m);
    c.add(minutes);
    return c.show();
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。