一、问题描述
试题编号: | 201803-2 |
试题名称: | 碰撞的小球 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处。有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒。 提示 因为所有小球的初始位置都为偶数,而且线段的长度为偶数,可以证明,不会有三个小球同时相撞,小球到达线段端点以及小球之间的碰撞时刻均为整数。 输入格式 输入的第一行包含三个整数n, L, t,用空格分隔,分别表示小球的个数、线段长度和你需要计算t秒之后小球的位置。 输出格式 输出一行包含n个整数,用空格分隔,第i个整数代表初始时刻位于ai的小球,在t秒之后的位置。 样例输入 3 10 5 样例输出 7 9 9 样例说明 初始时,三个小球的位置分别为4, 6, 8。 样例输入 10 22 30 样例输出 6 6 8 2 4 0 4 12 10 2 数据规模和约定 对于所有评测用例,1 ≤ n ≤ 100,1 ≤ t ≤ 100,2 ≤ L ≤ 1000,0 < ai < L。L为偶数。 |
二、解决方案
package com.lut.wy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Test {
class Boll{
public int no;
public int local;
public int dire;//0--左 1---右
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public int getDire() {
return dire;
}
public void setDire(int dire) {
this.dire = dire;
}
public int getLocal() {
return local;
}
public void setLocal(int local) {
this.local = local;
}
}
public static void main(String[] args) {
List<Boll> bolls =new ArrayList<>();
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int l=sc.nextInt();
int t=sc.nextInt();
if(sc.hasNextLine()) {
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
int j=0;
for (int local :a) {
Boll boll = new Test().new Boll();
boll.setLocal(local);
boll.setNo(j+1);
boll.setDire(1);
bolls.add(boll);
j++;
}
List<Boll> bos = new ArrayList<>();
bos=sortLocal(bolls);
List<Boll> reb=sortNo( handle(bos,l,t));
for (Boll boll : reb) {
System.out.print(boll.getLocal()+" ");
}
}
}
public static List<Boll> handle(List<Boll> bolls,int l,int time) {
for(int t=1;t<=time;t++) {
if(bolls.get(0).getLocal()==0) {
bolls.get(0).setDire(1);
bolls.get(0).setLocal(bolls.get(0).getLocal()+1);
}else {
if(bolls.get(0).getDire()==0) {
bolls.get(0).setLocal(bolls.get(0).getLocal()-1);
}else {
bolls.get(0).setLocal(bolls.get(0).getLocal()+1);
}
}
if(bolls.get(bolls.size()-1).getLocal()==l) {
bolls.get(bolls.size()-1).setDire(0);
bolls.get(bolls.size()-1).setLocal(bolls.get(bolls.size()-1).getLocal()-1);
}else {
if(bolls.get(bolls.size()-1).getDire()==1) {
bolls.get(bolls.size()-1).setLocal(bolls.get(bolls.size()-1).getLocal()+1);
}else {
bolls.get(bolls.size()-1).setLocal(bolls.get(bolls.size()-1).getLocal()-1);
}
}
for (int i = 1; i < bolls.size()-1; i++) {
if(bolls.get(i).getLocal() != bolls.get(i-1).getLocal() && bolls.get(i).getDire()==0) {
bolls.get(i).setLocal(bolls.get(i).getLocal()-1);
}
if(bolls.get(i).getLocal() != bolls.get(i+1).getLocal() && bolls.get(i).getDire()==1) {
bolls.get(i).setLocal(bolls.get(i).getLocal()+1);
}
if(bolls.get(i).getLocal()==bolls.get(i-1).getLocal()) {
bolls.get(i).setDire(1);
bolls.get(i-1).setDire(0);
}
if(bolls.get(i).getLocal()==bolls.get(i+1).getLocal()) {
bolls.get(i).setDire(0);
bolls.get(i+1).setDire(1);
}
}
}
return bolls;
}
public static List<Boll> sortLocal(List<Boll> bolls) {
Collections.sort(bolls, new Comparator<Boll>() {
@Override
public int compare(Boll o1, Boll o2) {
// TODO Auto-generated method stub
return o1.getLocal()-o2.getLocal();
}
});
return bolls;
}
public static List<Boll> sortNo(List<Boll> bolls) {
Collections.sort(bolls, new Comparator<Boll>() {
@Override
public int compare(Boll o1, Boll o2) {
// TODO Auto-generated method stub
return o1.getNo()-o2.getNo();
}
});
return bolls;
}
}