Linear world POJ - 2674(弹性碰撞+技巧)

题意:

给你n个居民的起始位置,各自在长度为l的平台,以同样的速度向左或向右走,当碰见时往相反方向走,问最后掉下去的居民花费的时间以及姓名。

题目:

The Disc, being flat, has no real horizon. Any adventurous sailors who get funny ideas from staring at eggs and oranges for too long and set out for the antipodes soon learned that the reason why distant ships sometimes looked as though they were disappearing over the edge of the world was that they were disappearing over the edge of the world. (Terry Pratchett -Colour of Magic)
Not so long time ago people used to believe that they live on 2-D world and if they will travel long enough in one direction, they will fall down over the edge. Even when it was proved that the Earth is rounded some of them were still afraid to travel to the southern hemisphere.
Try to imagine one 1-D (linear) world. On such world there are only two possible directions (left and right). All inhabitants of such world were created exactly at the same time and suddenly all of them start to move (all with same constant velocity) in one or the other direction. If two inhabitants encounter each other, they politely exchange greetings and then they turn around and start to move in an opposite direction. When an inhabitant reaches the end of the world he falls away and disappears.
Your task is to determine, for a given scenario of creation, which inhabitant and when (counting from the moment of creation) will be the last one to fall away. You can assume that the time required to exchange greetings and turn around is 0.

Input

The input consists of multiple descriptions (data sets) of the creation moment. File structure is as follows:
N
LV
DIR POS NAME

The first line defines the number of inhabitants (N<32000). Data set starting with value N=0 represents the end of the input file. The second line contains length of the world L(float) and velocity of inhabitants V(float). Both values are always positive. In next N lines the data about inhabitants are given in an order of increasing POS (positive direction):
DIR – initial direction (‘p’ or ‘P’ for positive and ‘n’ or ‘N’ for negative)
POS – position in the time of creation (0<=POS<=L)
NAME – name of inhabitant (string up to 250 characters)
Input values within one line are separated with at least one space and there will be no empty lines in input. You may assume that input is always correct and that each data set has only one unique solution.

Output

The output consists of one line per each input data set. The first value should be the time when the last inhabitant will fall of the linear world counting from the moment of creation. Value should be printed truncated to two decimal places in a field 13 characters wide. The second value should be the name of the inhabitant. Values should be separated with single space character.

Sample Input

1
13.5 2
p 3.5 Smarty
4
10 1
p 1 Helga
n 3 Joanna
p 5 Venus
n 7 Clever
0

Sample Output

     5.00 Smarty9.00 Venus

分析:

1.这道题求花费最长时间不难,遍历一遍即可找到,难点在于找到最后一个掉下去人的姓名。
2.我们可以认为蚂蚁相遇后名字会互相交换,并且擦肩而过,假设行进时间最长的蚂蚁为A,我们只需要关注在A的行进方向上有多少个和A反向的,就能知道会有多少只蚂蚁和真正的A碰头,但其实真正和A碰头的并不是最初和A反向的那些,而是在A初始方向上最靠近A的那几只蚂蚁,至于最终真正的A会带着谁的名字,只需要求出A的初始方向上有多少个初始方向和A反向的蚂蚁就行了,假设有cnt只,那么真正的A最后会携带A的初始方向上从A开始往前数cnt个的那只蚂蚁的名字,画画图就明白了。
3.需要注意,起始ma要初始化为-1,因为可能存在0.0;
4.因为是直接截取的小数点后两位,不是四舍五入,就不能直接%.2f,类似这么求,可以*100后强制转换一下再/100;
5.输出用%lf和%f的问题(后面有详细介绍)。
6.有时候题意没有分析到位就容易忽略条件,像我这样的英语战五渣就忽略了两个条件1.居民位置以递增的序列给出;2.输出数字应在13个字符宽的字段中被截断到小数点后两位。
题不算难,但容易坑,就这样吧,还是平时的一些习惯没有好好养成,wa了一晚上。。。。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,a,b,loc;
double l,v,ma;
bool flag;
struct node{double x;char direction[5];char na[255];
}s[32010];
int main(){while(~scanf("%d",&n)&&n){ma=-1;//起始ma要初始化为-1,因为可能存在0.0;a=b=0;scanf("%lf%lf",&l,&v);flag=false;for(int i=0;i<n;i++){scanf("%s%lf%s",&s[i].direction,&s[i].x,s[i].na);if(s[i].direction[0]=='p'||s[i].direction[0]=='P'){if(ma<l-s[i].x){ma=l-s[i].x;loc=i;flag=true;}}else {if(ma<s[i].x){ma=s[i].x;loc=i;flag=false;}}}if(flag){for(int i=loc;i<n;i++){if(s[i].direction[0]=='n'||s[i].direction[0]=='N')a++;}loc+=a;}else{for(int i=loc;i>=0;i--){if(s[i].direction[0]=='p'||s[i].direction[0]=='P')b++;}loc-=b;}printf("%13.2f %s\n",(int)(ma*100/v)/100.0,s[loc].na);}return 0;
}

关于输出用%lf和%f的问题

问:有人告诉我不能在printf中使用%lf。为什么printf()用%f输出double型,而scanf却用%lf呢?
答:printf的%f说明符的确既可以输出float型又可以输出double型。根据“默认参数提升”规则(在printf这样的函数的可变参数列表中,不论作用域内有没有原型,都适用这一规则)float型会被提升为double型。因此printf()只会看到双精度数。(严格地讲,%lf在printf下是未定义的,但是很多系统可能会接受它。要确保可移植性,就要坚持使用%f。)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309527.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

TechEmpower Web 框架性能第19轮测试结果正式发布,ASP.NET Core在主流框架中拔得头筹...

TechEmpower第19轮编程语言框架性能排行榜2020年5月28日正式发布,详见官方博客&#xff1a;https://www.techempower.com/blog/2020/05/28/framework-benchmarks-round-19/&#xff0c;TechEmpower基准测试有许多场景&#xff08;也称为测试类型&#xff09;&#xff0c;此次评…

Calendar Game POJ - 1082(关于日历的博弈问题)

题意&#xff1a; 两个人轮流玩游戏&#xff0c;每个人可以把日期进行转移&#xff0c;转移规则&#xff1a; 1.可以转移到当前日期的下一天。 2可以转移到下个月的这一天。&#xff08;但是如果下个月没有这一天就不能进行第二种转移&#xff09; 3.如果A恰好移动到2001.11.4…

[Java基础]JDK内置注解

示例代码如下: package annotation;SuppressWarnings("all") public class AnnoDemo2 {Overridepublic String toString(){return super.toString();}Deprecatedpublic void show(){//有缺陷}public void show1(){//替代show方法}public void demo(){show();}}

学了这么多年精益思想,居然不知道还有第八种浪费 | IDCF

价值和浪费“我是彻底的现场主义者。与其在领导办公室内冥思苦想&#xff0c;倒不如到生产现场的各个角落&#xff0c;直接获得第一手的生产信息和感受直接的刺激。”——大野耐一&#xff0c;丰田生产方式之父大野耐一提倡直接去现场工作&#xff0c;在那里才能看到价值与浪费…

41 sysfs 文件系统

前言 在 linux 中常见的文件系统 有很多, 如下 基于磁盘的文件系统, ext2, ext3, ext4, xfs, btrfs, jfs, ntfs 内存文件系统, procfs, sysfs, tmpfs, squashfs, debugfs 闪存文件系统, ubifs, jffs2, yaffs 文件系统这一套体系在 linux 有一层 vfs 抽象, 用户程序不用…

A Greeting from Qinhuangdao Gym - 102769A 2020ccpc秦皇岛分站赛

题意&#xff1a; 给你n个红球和m个蓝色球。然后以相等的概率随机选择了其中两个。选择两个红球的概率是多少&#xff1f; 题目&#xff1a; Welcome to the CCPC Qinhuangdao Site! Qinhuangdao is a beautiful coastal city full of charm, integrating historical herit…

Gartner:6个容器和Kubernetes策略的最佳实用技巧

导语采用容器和Kubernetes要求整个企业保持一致&#xff0c;不了解这些前期现实会导致一些非常严峻的后果。正文Gartner估计&#xff0c;到2022年&#xff0c;将有75&#xff05;的组织在生产中运行容器化应用程序。毫无疑问&#xff0c;Kubernetes已成为组织容器的流行方法。通…

[JavaWeb-MySQL]约束(非空约束,唯一约束,主键约束,外键约束_级联操作)

约束 * 概念&#xff1a; 对表中的数据进行限定&#xff0c;保证数据的正确性、有效性和完整性。 * 分类&#xff1a;1. 主键约束&#xff1a;primary key2. 非空约束&#xff1a;not null3. 唯一约束&#xff1a;unique4. 外键约束&#xff1a;foreign key* 非空约束&#x…

Friendly Group Gym - 102769F 2020(并查集)ccpc秦皇岛分站赛

题意&#xff1a; n个学生要组成一个小组参加会议&#xff08;可以不参加&#xff09;&#xff0c; 1.对于每两个朋友&#xff08;x &#xff0c;y)&#xff0c;如果他们俩都参加会议&#xff0c;该小组的友好价值将会增加 1&#xff1b;如果其中只有一位参加会议&#xff0c;…

测试人员未来的3条出路

大家好&#xff0c;我是Z哥。前两天有个做测试的小伙伴加我微信问我测试相关的一些事情。她自己是从学习毕业就开始进入到互联网行业做测试的&#xff0c;到现在三年工作经验。她现在都不太敢跳槽&#xff0c;因为觉得自己没有什么核心竞争力&#xff0c;平常就是点点鼠标&…

[JavaWeb-MySQL]DQL_查询表中记录,语句

DQL&#xff1a;查询表中的记录 * select * from 表名;1. 语法&#xff1a;select字段列表from表名列表where条件列表group by分组字段having分组之后的条件order by排序limit分页限定2. 基础查询1. 多个字段的查询select 字段名1&#xff0c;字段名2... from 表名&#xff1b…

团体程序设计天梯赛-练习集L1-025 正整数A+B (15分)(getline输入)

题目&#xff1a; 题的目标很简单&#xff0c;就是求两个正整数A和B的和&#xff0c;其中A和B都在区间[1,1000]。稍微有点麻烦的是&#xff0c;输入并不保证是两个正整数。 输入格式&#xff1a; 输入在一行给出A和B&#xff0c;其间以空格分开。问题是A和B不一定是满足要求…

自定义值类型一定不要忘了重写Equals,否则性能和空间双双堪忧

一&#xff1a;背景1. 讲故事曾今在项目中发现有同事自定义结构体的时候&#xff0c;居然没有重写Equals方法&#xff0c;比如下面这段代码&#xff1a;static void Main(string[] args){var list Enumerable.Range(0, 1000).Select(m > new Point(m, m)).ToList();var ite…

[JavaWeb-MySQL]DDL_操作数据库,表

DDL:操作数据库、表 1. 操作数据库&#xff1a;CRUD1. C(Create):创建* 创建数据库&#xff1a;* create database 数据库名称;* 创建数据库&#xff0c;判断不存在&#xff0c;再创建&#xff1a;* create database if not exists 数据库名称;* 创建数据库&#xff0c;并指定…

Division CodeForces - 1445C(数论因子相关)

题意&#xff1a; 找一个最大的数X&#xff0c;使p%x0且x%q!0&#xff0c;题目保证至少有一个答案满足题意。 题目&#xff1a; Oleg’s favorite subjects are History and Math, and his favorite branch of mathematics is division. To improve his division skills, O…

使用 Windows Terminal 连接远程主机

使用 Windows Terminal 连接远程主机IntroWindows Terminal 是微软新推出来的一个全新的、流行的、功能强大的命令行终端工具。包含很多来社区呼声很高的特性&#xff0c;例如&#xff1a;多 Tab 支持、富文本、多语言支持、可配置、主题和样式&#xff0c;支持 emoji 和基于 G…

[JavaWeb-MySQL]DML_操作表

DML&#xff1a;增删改表中数据 1. 添加数据&#xff1a;* 语法&#xff1a;* insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);* 注意&#xff1a;1. 列名和值要一一对应。2. 如果表名后&#xff0c;不定义列名&#xff0c;则默认给所有列添加值insert into …

.Net Core实现区块链初探

区块链这么火&#xff0c;咱也跟个风。一、前言最近&#xff0c;银行总行关于数字货币即将推出的消息频传&#xff0c;把BTC也带得来了一波反弹。借着这个风&#xff0c;我们也研究一下区块链。通常大家说到区块链&#xff0c;实际包括两部分概念&#xff1a;第一个概念&#x…