POJ 2501 Average Speed(水~)

470 篇文章 3 订阅

Description
汽车在马路上行驶,假设匀速,不考虑变速过程对路程的影响,先给出几个时刻,若是输入此时的速度则表示汽车在此时变速,否则则需输出汽车此时行驶的总路程(速度单位是km/h)
Input
一组用例,给出多个时刻,以文件尾结束输入
Output
若汽车在某时刻没有变速,则输出汽车此时行驶的总路程
Sample Input
00:00:01 100
00:15:01
00:30:01
01:00:01 50
03:00:01
03:00:05 140
Sample Output
00:15:01 25.00 km
00:30:01 50.00 km
03:00:01 200.00 km
Solution
水题,注意时刻和速度的输入以及时刻的转化
Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int cal(int h,int m,int s)
{
    return 3600*h+60*m+s;
}
int main()
{
    int h,m,s;
    double speed=0,ans=0;
    int last=0,now;
    while(scanf("%d:%d:%d",&h,&m,&s)!=EOF)
    {
        char c=getchar();
        if(c==' ')//判断是否变速 
        {
            int d;
            scanf("%d",&d);
            now=cal(h,m,s);
            ans+=(now-last)*speed;//在这个时间段行驶的路程 
            last=now;//更新上一个时刻 
            speed=d/3.6;//速度的单位转化 
        }
        else
            printf("%02d:%02d:%02d %.2lf km\n",h,m,s,(ans+(cal(h,m,s)-last)*speed)/1000);//路程的单位转化 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值