百词斩笔试题:一天内时针、分针、秒针转过的角度

题目描述

给定一天内先后的两个时间,求时针、分针、秒针在时钟表盘上转动了多少角度。
注意:为了保证计算精度,请使用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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值