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

Waterloo local 2001.09.22

分析:

模拟,注意一开始可能就问,或者一开始赋予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\n",havdrive+(time-nowtime)/3600*nowspeed);//写成printf(" %.2f km\n",havdrive+(time-nowtime)/3600*nowspeed)就错了!!
26         }
27     }
28     return 0;
29 }
时间: 2024-10-21 21:11:21

poj 2501 Average Speed的相关文章

POJ 2501 Average Speed(不错的一道水题)

[题目简述]:给出我们时间和速度,让我们求出走了多远的距离 [分析]:这道题开始的时候没有太明白什么时候输出,后来看了别人的题解就明白了. 关于此题的几点总结: 1.时间的输入方法:scanf("%d:%d:%d",&h,&m,&s),注意积累! 2.关于空格的的输入控制使用char ch = getchar(),同时它还作为了本题的一个是否输出的标识控制的条件. 3.多积累类似题目的方法. 代码参考http://blog.csdn.net/yujuan_mao

POJ 2501

1 #include<iostream> 2 #include<iomanip> 3 #include<stdio.h> 4 #include<string> 5 #include<math.h> 6 using namespace std; 7 8 int main() 9 { 10 //freopen("acm.acm","r",stdin); 11 string s; 12 int i; 13 dou

HDU 3128 What is the air speed velocity…(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3128 Problem Description What is the air speed velocity- -of a fully laden swallow? This fearful question was posed to the intrepid band of Grail searchers.Their response of "African or European?" w

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

Redis集群搭建

Redis集群是一个提供在多个Redis节点间共享数据的程序集. Redis集群中不支持处理多个keys的命令. Redis集群通过分区来提供一定程度的可用性.在某个节点宕机或者不可用的时候可以继续处理命令. Redis集群数据分片 在Redis集群中,使用数据分片(sharding)而不是一致性hash(consistency hashing)来实现,一个Redis集群包含16384个哈希槽(hash slot),数据库中的每个键都存在这些哈希槽中的某一个,通过CRC16校验后对16384取模

zabbix--3.0--1

实验环境 实验用2到2台机器,实验所用机器系统环境如下,可以看到2台机器的主机名和IP地址 [[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) [[email protected] ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 l

curl 命令获取网络网站的响应码

curl命令参数很多,博主很多都没有用过.今天发现可以用-w参数挺好用的. -w:--write-out,作用就是输出点什么.curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出.输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{variable_name}的格式,如果需要输出%,double一下即可,即%%,同时,\n是换行,\r是回车,\t是TAB.curl会用合适的值来替代输出格式中的变量,所有可用变量如下: url_effective 最终获取的url地

每天一个linux命令13之curl发送http请求

一.get请求 curl "http://www.baidu.com"  如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http://www.baidu.com"  显示全部信息 curl -l "http://www.baidu.com" 只显示头部信息 curl -v "http://www.baidu.com" 显示get请求全过程解析 wget "http://www.ba

etcd api 接口

etcd api接口 采用标准的restful 接口,支持http 和 https 两种协议. 1. PUT 为etcd存储的键赋值, 即创建 message 键值,赋值为"Hello world" 1 [[email protected] ~]# curl http://127.0.0.1:2379/v2/keys/message -X PUT -d value="Hello world" | python -m json.tool 2 % Total % Rec