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.感想:
就是这题,坑爹死了,卡了我好久,刚开始没有想到要用求模的,因为题目的数据范围,恶心死了,动买叼!
在此做个纪念。