Collision detection
Time Limit:1000MS Memory Limit:65536K
Total Submit:44 Accepted:17
Description
Give you a rectangle,and the upper left point is(0,0),the lower right point is (n,m).There is one ball at (x0,y0) in the rectangle,and its' initial speed use the given x and y direction speed to represent, were the xv and yv.(per second)
Now, we want to know where is the ball after t seconds. We can guarantee the ball will occurred a perfect elastic collision with the wall.
设置一个矩形的坐标,已知其中一个端点的坐标为(0,0),输入另一个端点的坐标;
输入小球在矩形中的原始位置坐标
输入小球在x轴与y轴上的速度值
输入时间t
(attention:要求输入值必须都为整数,这里必须注意包含负整数)
输出时间t后小球的坐标
小球在与边框发生碰撞时只发生完全弹性碰撞
Input
Multiple test cases.
First line , input two integers n , m (10<=n,m<=500)
Second line , input two integers x0 , y0 represent the coordinate of the ball.(1<=x0<=n , 1<=y0<=m)
Third line , there are two integers xv , yv represent the speed in x direction and the speed in y direction .(0<=xv<=n , 0<=yv<=m)
In the last line , there is a integer t stand for the time.
All of the input value are integer.
Output
Two integers , represent the coordinate of the ball after t seconds.
Sample Input
10 10
1 1
4 6
3
Sample Output
7 1
Hint
If the ball just crashed into the corner , it will go back in the
reverse direction
最开始的时候我没有注意到整数的界限,仅仅考虑的正数的情况
代码如下
#include <stdio.h>
int main()
{
int flag,n,m,x0,y0,vx,vy,t,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%d%d",&x0,&y0);
scanf("%d%d",&vx,&vy);
scanf("%d",&t);
flag=(x0+vx*t)/n;
if(flag%2==0)
x=(x0+vx*t)%n;
else if(flag%2==1)
x=n-(x0+vx*t)%n;
flag=(y0+vy*t)/m;
if(flag%2==0)
y=(y0+vy*t)%m;
else if(flag%2==1)
y=m-(y0+vy*t)%m;
printf("%d %d\n",x,y);
}
return 0;
}
//这样的一个程序仅仅满足的当坐标及速度均为正值的情况,当负数存在时计算就会发生错误
矩形坐标具有四种可能正负组合
初始坐标四种可能正负组合
初始速度可能有四种可能正负组合
若是不考虑别的情况直接想的话会有六十四种可能 显然这其中是有规律的