201803-2 碰撞的小球

问题描述

数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处。有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒。

当小球到达线段的端点(左端点或右端点)的时候,会立即向相反的方向移动,速度大小仍然为原来大小。

当两个小球撞到一起的时候,两个小球会分别向与自己原来移动的方向相反的方向,以原来的速度大小继续移动。

现在,告诉你线段的长度L,小球数量n,以及n个小球的初始位置,请你计算t秒之后,各个小球的位置。


提示
因为所有小球的初始位置都为偶数,而且线段的长度为偶数,可以证明,不会有三个小球同时相撞,小球到达线段端点以及小球之间的碰撞时刻均为整数。

同时也可以证明两个小球发生碰撞的位置一定是整数(但不一定是偶数)。

输入格式
输入的第一行包含三个整数n, L, t,用空格分隔,分别表示小球的个数、线段长度和你需要计算t秒之后小球的位置。

第二行包含n个整数a1, a2, …, an,用空格分隔,表示初始时刻n个小球的位置。

输出格式
输出一行包含n个整数,用空格分隔,第i个整数代表初始时刻位于ai的小球,在t秒之后的位置。

样例

样例1

输入
3 10 5
4 6 8
输出
7 9 9

样例2

输入
10 22 30
14 12 16 6 10 2 8 20 18 4
输出
6 6 8 2 4 0 4 12 10 2

java代码

import java.util.*;

class Ball{
	int locate;
	int direction;
	int num;
	Ball(String l, int n){
		locate = Integer.parseInt(l);
		direction = 1;
		num = n;
	}
	Ball(int l, int d, int n){
		locate = l;
		direction = d;
		num = n;
	}
	public int GetLocate() {
		return locate;
	}
	public void Bounce() {
		direction = -1 * direction;
	}
	public void Move() {
		locate += direction;
	}
	public int GetDir() {
		return direction;
	}
	public int GetNum() {
		return num;
	}
	public void copy(Ball b) {
		locate = b.GetLocate();
		direction = b.GetDir();
		num = b.GetNum();
	}
}
public class Main {
	public static void sortBall(List<Ball> ball) {
		int i, j;
		for(j = 0; j < ball.size(); j++) {
			for(i = 0; i < ball.size() - 1 - j; i++) {
				if(ball.get(i).GetLocate() > ball.get(i + 1).GetLocate()) {
					Ball temp = new Ball(ball.get(i).GetLocate(), ball.get(i).GetDir(), ball.get(i).GetNum());
					ball.get(i).copy(ball.get(i + 1));
					ball.get(i + 1).copy(temp);
				}
			}
		}
	}
	public static void initBall(List<Ball> ball) {
		int i, j;
		for(j = 0; j < ball.size(); j++) {
			for(i = 0; i < ball.size() - 1 - j; i++) {
				if(ball.get(i).GetNum() > ball.get(i + 1).GetNum()) {
					Ball temp = new Ball(ball.get(i).GetLocate(), ball.get(i).GetDir(), ball.get(i).GetNum());
					ball.get(i).copy(ball.get(i + 1));
					ball.get(i + 1).copy(temp);
				}
			}
		}
	}
	public static void main(String[] args) {
		int i, j;
		Scanner sc = new Scanner(System.in);
		String all = sc.nextLine();
		String[] temp1 = all.split(" ");
		int n = Integer.parseInt(temp1[0]);
		int L = Integer.parseInt(temp1[1]);
		int t = Integer.parseInt(temp1[2]);
		List<Ball> ball = new ArrayList<>();
		all = sc.nextLine();
		String[] temp2 = all.split(" ");
		for(i = 0; i < n; i++) {
			Ball b = new Ball(temp2[i], i);
			ball.add(b);
		}
		sortBall(ball);
		for(j = 0; j < t; j++) {
			for(i = 0; i < n - 1; i++) {
				if(ball.get(i).GetLocate() == 0 || ball.get(i).GetLocate() == L) {
					ball.get(i).Bounce();
				}
				if(ball.get(i).GetLocate() == ball.get(i + 1).GetLocate()) {
					ball.get(i).Bounce();
					ball.get(i + 1).Bounce();
				}
			}
			if(ball.get(i).GetLocate() == 0 || ball.get(i).GetLocate() == L) {
				ball.get(i).Bounce();
			}
			for(i = 0; i < n; i++)
				ball.get(i).Move();
			sortBall(ball);
		}
		initBall(ball);
		System.out.print(ball.get(0).GetLocate());
		for(i = 1; i < n; i++) {
			System.out.print(" " + ball.get(i).GetLocate());
		}
		System.out.println();
	}
}

其他

总觉得自己的做法有点复杂hmmmm
周日就考试了好紧张啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值