时钟类

1.题目:

Problem Description

定义一个时钟类(Clock),含三个整形元素(H,M,S)分别代表小时,分钟,秒,构造初始化数据函数,另外,定义一个函数实现两个时钟相加,一个显示函数,输出格式为H:M:S

Input

输入数据有多组,每组两行,第一行输入三个整数:h1(0<=h1<=23),m1(0<=m1<=59),s1(0<=s1<=59),分别代表是一个24小时制的电子钟开始时显示的小时,分钟,秒,第二行仍输入三个整数:h2(h2>=0),m2(0<=m2<=59),s1(0<=s2<=59),代表电子钟经历的时长

Output

每组输出占一行,输出现在电子钟上显示的时间,格式为“小时:分钟:秒”,注意:此题不考虑电子钟显示00~09的形式,用0~9表示即可。

Sample Input

13 30 30
1 10 30
1 0 0
25 0 0

Sample Output

14:41:0
2:0:0

2.参考代码:

#include <iostream>
using namespace std;

class CLOCK
{
    
private:
    int hour, minute, second;
    
public:
    CLOCK(int h = 0, int m = 0, int s = 0);
    CLOCK operator+(CLOCK&);
    void show();
    
};

CLOCK::CLOCK(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s;
}

CLOCK CLOCK::operator+(CLOCK& c)
{
    CLOCK x;
    x.hour = hour + c.hour;
    x.minute = minute + c.minute;
    x.second = second + c.second;
    if (x.second / 60)
        x.minute++;
    if (x.minute / 60)
        x.hour++;
    x.second %= 60;
    x.minute %= 60;
    x.hour %= 24;
    return x;
}

void CLOCK::show()
{
    cout << hour << ":" << minute << ":" << second << endl;
}

int main()
{
    int h1, m1, s1, h2, m2, s2;

    while (cin >> h1 >> m1 >> s1 >> h2 >> m2 >> s2) {
        CLOCK x(h1, m1, s1), y(h2, m2, s2), z;
        z = x + y;
        z.show();
    }

    return 0;
}


 

3.感想:

        就是这题,坑爹死了,卡了我好久,刚开始没有想到要用求模的,因为题目的数据范围,恶心死了,动买叼!

在此做个纪念。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值