poj3684:Physics Experiment

来源:互联网 发布:装网络摄像头怎么安装 编辑:程序博客网 时间:2024/06/05 21:07

Description
Simon is doing a physics experiment with N identical balls with the same radius of R centimeters. Before the experiment, all N balls are fastened within a vertical tube one by one and the lowest point of the lowest ball is H meters above the ground. At beginning of the experiment, (at second 0), the first ball is released and falls down due to the gravity. After that, the balls are released one by one in every second until all balls have been released. When a ball hits the ground, it will bounce back with the same speed as it hits the ground. When two balls hit each other, they with exchange their velocities (both speed and direction).
这里写图片描述
Simon wants to know where are the N balls after T seconds. Can you help him?
In this problem, you can assume that the gravity is constant: g = 10 m/s2.

Input
The first line of the input contains one integer C (C ≤ 20) indicating the number of test cases. Each of the following lines contains four integers N, H, R, T.
1≤ N ≤ 100.
1≤ H ≤ 10000
1≤ R ≤ 100
1≤ T ≤ 10000

Output
For each test case, your program should output N real numbers indicating the height in meters of the lowest point of each ball separated by a single space in a single line. Each number should be rounded to 2 digit after the decimal point.

Sample Input
2
1 10 10 100
2 10 10 100

Sample Output
4.95
4.95 10.20

题目大意
在H米高处设置一个圆筒,里面有n个半径为R的球(第i个球距离地面H+2*(i-1)*R)。 第0秒时,最下面的求开始下落,此后每一秒有一个球下落。不计空气阻力,球与球之间,球与地面之间的弹撞是弹性碰撞。 问经过T秒后,每个球底端距地面的高度?按顺序输出。

解题思路
这里写图片描述
分三个过程来看:
1,假设所有的小球都是没有半径的,且从同一高度每隔1s掉下。这个时候跟蚂蚁是几乎差不多的,碰了等于没碰。

2.假设两个小球是有半径的,且是紧挨着每隔1s落下,这时候,因为小球有半径,其实碰了还是等于没碰。a,b两球碰撞时,由速度交换可知,可以当做a向上瞬移2*r,且保持原来的速度,b向下瞬移2*r,且可保持原来的速度,那么a能够达到的最大高度变成了原来的b的初始位置(假设b原来放在a的上方),b能够达到的最大高度就变成了a(因为瞬移的结果是增大了a的2*h的势能,削弱了b的2*h的势能),最终的结果是a变成了原来的b球,b变成了原来的a球,总的效果其实就是没有碰撞,多个球于此类似。其实核心思想是,每次碰撞时,a球起初(刚释放时)势能要比b球少2*r(因为相对顺序不变),而碰撞后的瞬移使得a球增加了2*r的势能,b球减少了2*r的势能,因此最后变成了a比b还多了2*r的势能,也就是a,b的互换了。

3.其实这样做的出来了,不过因为每个球高度不同,计算不方便。为了计算简便,可以再来一个简化,假设两个球紧挨着同一时刻一起下落会怎样?显然,在上面的球高度始终要比下面的球高2×r,因此计算从高为H的位置落下的球,然后其他球累加2*r就好。
代码如下:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<iomanip>using namespace std;const double g=10.0;int N,H,R,T;double y[105];double calc(int T){    if(T<0)    return H;    double t=sqrt(2*H/g);    int k=(int)(T/t);    if(k%2==0)    {        double d=T-k*t;        return H-g*d*d/2;    }    else    {        double d=k*t+t-T;        return H-g*d*d/2;    }}void solve(){    for(int i=0;i<N;i++)    y[i]=calc(T-i);    sort(y,y+N);    for(int i=0;i<N-1;i++)    cout<<setiosflags(ios::fixed)<<setprecision(2)<<y[i]+2*i*R/100.0<<" ";    cout<<setiosflags(ios::fixed)<<setprecision(2)<<y[N-1]+2*R*(N-1)/100.0<<endl;}int main(){    int t;    cin>>t;    while(t--)    {        cin>>N>>H>>R>>T;        memset(y,0,sizeof(y));        solve();    }    return 0;}