0%

北大 acm 2501 Average Speed 解题报告

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3225 Accepted: 1401

Description

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

Input

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.

Output

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

Sample Input

00:00:01 10000:15:0100:30:0101:00:01 5003:00:0103:00:05 140

Sample Output

00:15:01 25.00 km00:30:01 50.00 km03:00:01 200.00 km

题意:输入时间,如果没有输入速度的话则输出行走的距离,如果输入时间和速度的话则不输出距离只设定速度

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include <cmath>

using namespace std;

int getTime(int& h,int &m,int& s,char str[]) //将字符前8位转化为时间
{
h=(str[0]-'0')*10+str[1]-'0';
m=(str[3]-'0')*10+str[4]-'0';
s=(str[6]-'0')*10+str[7]-'0';
return 0;
}

int main()
{
int h,m,s,speed=0,i,len;
float distance=0;
float pretime;
char str[100];

while(gets(str))
{
getTime(h,m,s,str);

len=strlen(str);
distance+=((s-1)*1.000/3600+m*1.000/60+h-pretime)*speed*1.000;

if(len>8) //如果长度大于8的话说明还输入了速度
{
speed=0;
for(i=len-1;i>=9;i--)
speed+=(str[i]-'0')*pow(10.0,len-1-i); //提取速度
}
else
{
for(i=0;i<8;i++)
cout<<str[i];

printf(" %.2f km\n",distance); //长度小于8就输出
}

pretime=(s-1)*1.000/3600+m*1.000/60+h*1.000;
}
return 0;
}
谢谢您的打赏,我的大英雄 ^_^
Thank you for your generosity, my big hero ^_^

更多内容请关注公众号「西小玛」