题目描述
给定一天内先后的两个时间,求时针、分针、秒针在时钟表盘上转动了多少角度。
注意:为了保证计算精度,请使用double类型进行计算。
输入描述
输入两行,分别代表两个时间点A和B,A和B都为24小时制,B大于等于A,且都在同一天,时分秒用冒号(:)分割
输出描述
输出为三行,分别代表时针、分针、秒针在表盘上转动的角度,结果只保留整数部分。请勿四舍五入。
实例
输入
00:00:00
18:00:00
输出
540
6480
388800
解题
思路:直接根据题意,算出时间差,小时差dh,分钟差dm,秒差ds。
则,时针的角度为:pha1 = (dh + dm/60 + ds/3600) * 30;
分钟的角度为:pha2 = (60dh + dm + ds/60) * 6
秒钟的角度为:pha3 = (3600 dh + 60*dm + ds) *6
C++字符串转数字可参考另外一篇博文
源代码:
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
string str1, str2;
int h1,m1,s1, h2,m2,s2;
int dh,dm,ds;
while(getline(cin,str1))
{
getline(cin,str2);
h1 = atof(str1.substr(0,2).c_str());
m1 = atof(str1.substr(3,2).c_str());
s1 = atof(str1.substr(6,2).c_str());
h2 = atof(str2.substr(0,2).c_str());
m2 = atof(str2.substr(3,2).c_str());
s2 = atof(str2.substr(6,2).c_str());
dh = h2 - h1;
dm = m2 - m1;
ds = s2 - s1;
cout << floor((dh + dm/60 + ds/3600)*30) << endl;
cout << floor((60*dh + dm + ds/60)*6) << endl;
cout << floor((3600*dh + dm*60 + ds)*6) << endl;
}
return 0;
}