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 }
  • 相关阅读:
    跨域导致FormsAuthentication.Decrypt报错:填充无效,无法被移除
    Php构造函数construct的前下划线是双的_
    DNN学习资源整理
    改进housemenu2使网站导航亲Seo并在新窗口中打开。
    推荐10款非常优秀的 HTML5 开发工具
    Ext.Net系列:安装与使用
    Devexpress 破解方法
    Microsoft Visual Studio 2010 遇到了异常,可能是由某个扩展导致的
    浮躁和互联网
    chrome 默认以 https打开网站
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4374456.html
Copyright © 2011-2022 走看看