日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/4 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linear world POJ - 2674(弹性碰撞+技巧) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題意:

給你n個(gè)居民的起始位置,各自在長度為l的平臺(tái),以同樣的速度向左或向右走,當(dāng)碰見時(shí)往相反方向走,問最后掉下去的居民花費(fèi)的時(shí)間以及姓名。

題目:

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.這道題求花費(fèi)最長時(shí)間不難,遍歷一遍即可找到,難點(diǎn)在于找到最后一個(gè)掉下去人的姓名。
2.我們可以認(rèn)為螞蟻相遇后名字會(huì)互相交換,并且擦肩而過,假設(shè)行進(jìn)時(shí)間最長的螞蟻為A,我們只需要關(guān)注在A的行進(jìn)方向上有多少個(gè)和A反向的,就能知道會(huì)有多少只螞蟻和真正的A碰頭,但其實(shí)真正和A碰頭的并不是最初和A反向的那些,而是在A初始方向上最靠近A的那幾只螞蟻,至于最終真正的A會(huì)帶著誰的名字,只需要求出A的初始方向上有多少個(gè)初始方向和A反向的螞蟻就行了,假設(shè)有cnt只,那么真正的A最后會(huì)攜帶A的初始方向上從A開始往前數(shù)cnt個(gè)的那只螞蟻的名字,畫畫圖就明白了。
3.需要注意,起始ma要初始化為-1,因?yàn)榭赡艽嬖?.0;
4.因?yàn)槭侵苯咏厝〉男?shù)點(diǎn)后兩位,不是四舍五入,就不能直接%.2f,類似這么求,可以*100后強(qiáng)制轉(zhuǎn)換一下再/100;
5.輸出用%lf和%f的問題(后面有詳細(xì)介紹)。
6.有時(shí)候題意沒有分析到位就容易忽略條件,像我這樣的英語戰(zhàn)五渣就忽略了兩個(gè)條件1.居民位置以遞增的序列給出;2.輸出數(shù)字應(yīng)在13個(gè)字符寬的字段中被截?cái)嗟叫?shù)點(diǎn)后兩位。
題不算難,但容易坑,就這樣吧,還是平時(shí)的一些習(xí)慣沒有好好養(yǎng)成,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,因?yàn)榭赡艽嬖?.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; }

關(guān)于輸出用%lf和%f的問題

問:有人告訴我不能在printf中使用%lf。為什么printf()用%f輸出double型,而scanf卻用%lf呢?
答:printf的%f說明符的確既可以輸出float型又可以輸出double型。根據(jù)“默認(rèn)參數(shù)提升”規(guī)則(在printf這樣的函數(shù)的可變參數(shù)列表中,不論作用域內(nèi)有沒有原型,都適用這一規(guī)則)float型會(huì)被提升為double型。因此printf()只會(huì)看到雙精度數(shù)。(嚴(yán)格地講,%lf在printf下是未定義的,但是很多系統(tǒng)可能會(huì)接受它。要確保可移植性,就要堅(jiān)持使用%f。)

總結(jié)

以上是生活随笔為你收集整理的Linear world POJ - 2674(弹性碰撞+技巧)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。