Average Speed
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4842 | Accepted: 2168 |
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 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
Source
分析:
模拟,注意一开始可能就问,或者一开始赋予0的速度。
getchar()判断是询问还是更新。
具体代码:
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<iostream> 7 #include<stack> 8 using namespace std; 9 double trans(string s){ 10 return ((s[0]-'0')*10+s[1]-'0')*3600+((s[3]-'0')*10+s[4]-'0')*60+(s[6]-'0')*10+s[7]-'0'; 11 } 12 int main(){ 13 string s; 14 double nowspeed=0,nowtime=0,speed,time,havdrive=0; 15 while(cin>>s){ 16 time=trans(s); 17 if(getchar()==' '){//速度更新,已行驶的路程更新,基准时间更新 18 cin>>speed; 19 havdrive+=(time-nowtime)/3600*nowspeed; 20 nowtime=time; 21 nowspeed=speed; 22 } 23 else{ 24 cout<<s; 25 printf(" %.2f km ",havdrive+(time-nowtime)/3600*nowspeed);//写成printf(" %.2f km ",havdrive+(time-nowtime)/3600*nowspeed)就错了!! 26 } 27 } 28 return 0; 29 }