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;
}