ZCMU-Average speed

Problem A: Problem A: Average Speed

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 54   Solved: 24
[ Submit][ Status][ Web Board]

Description

Problem A: Average Speed

You have bought a car in order to drive from Waterloo to a big city. The odometer on their car is broken, so you cannot measure distance. But the speedometer and cruise control both work, so the car can maintain a constant speed which can be adjusted from time to time in response to speed limits, traffic jams, and border queues. You have a stopwatch and note the elapsed time every time the speed changes. From time to time you wonder, "how far have I come?". To solve this problem you must write a program to run on your laptop computer in the passenger seat.

Standard input contains several lines of input: Each speed change is indicated by a line specifying the elapsed time since the beginning of the trip (hh:mm:ss), followed by the new speed in km/h. Each query is indicated by a line containing the elapsed time. At the outset of the trip the car is stationary. Elapsed times are given in non-decreasing order and there is at most one speed change at any given time.

For each query in standard input, you should print a line giving the time and the distance travelled, in the format below.

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

【解析】
刚开始没看懂题目之后才看懂的..毕竟英语差,其实这道题是这样子的如果输入格式是这样的话00:00:01 100后面
100代表的是速度,这样我们就不用查询,如果输入的是00:15:01那我们就应该查询,这个时候我们怎么查询呢显然
我们需要一个变量来存储时间,时间的计算我们最好换成秒数来进行。如果我们要改变速度了,就拿改变速度的时间
减去一开始存储的时间,改变速度的时候sum+加的是改变速度的时刻的。不改变速度的查询加上的是改变速度的到最
前面给出的速度的那一段距离。
#include<iostream>
#include<string>
#include<map>
#include<cstring>
#include<cstdio>
using namespace std;
int facs(int h,int m,int s)
{
    return h*3600+m*60+s;
}
int main()
{
    int n,m,i,j,d,p,q,v;
    int now=0,last=0;
    double k=0,sum,sum1;
    char c;
    while(~scanf("%d:%d:%d",&p,&q,&v))
    {
        c=getchar();
        if(c==' ')
        {
            scanf("%d",&d);
            now=facs(p,q,v);
            sum+=(now-last)*k;//这个距离是表示上一个改变速度的时刻到现在改变速度的时刻走了多远。
            k=d/3.6;//这个时候如果是100公里每小时那就是100000/3600,表示1s多少米
            last=now;//记录上一个时刻
        }
        else
        {
            sum1=((facs(p,q,v)-last)*k+sum)/1000;
            printf("%02d:%02d:%02d %.2f km\n",p,q,v,sum1);
        }
    }
     return 0;
}

改变速度的时间加上
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值