zoukankan      html  css  js  c++  java
  • poj 2501 Average Speed

    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 }
  • 相关阅读:
    C++中几个值得分析的小问题(1)
    《Effective C++》第5章 实现-读书笔记
    《Effective C++》第4章 设计与声明(2)-读书笔记
    《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(2)-读书笔记
    《Effective C++》第4章 设计与声明(1)-读书笔记
    《Effective C++》第3章 资源管理(2)-读书笔记
    【剑指Offer】24二叉树中和为某一值的路径
    【剑指Offer】23二叉搜索树的后序遍历序列
    【剑指Offer】22从上往下打印二叉树
    【剑指Offer】21栈的压入、弹出序列
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4374456.html
Copyright © 2011-2022 走看看