- 问题链接:碰撞的小球
- 问题分析:模拟法解决。
- 程序说明:n, L, t分别表示小球的个数、线段长度和你需要计算t秒之后小球的位置;s数组存小球起始位置,v数组存小球速度
- 程序代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Reader {
static StringTokenizer token =new StringTokenizer("");
static BufferedReader reader =new BufferedReader (new InputStreamReader(System.in)) ;
static String nextLine() throws IOException {
return reader.readLine() ;
}
static String next() throws IOException {
while(!token.hasMoreTokens()) {
token =new StringTokenizer(reader.readLine()) ;
}
return token.nextToken() ;
}
static int nextInt() throws IOException {
return Integer.parseInt(next()) ;
}
static double nextDouble() throws IOException {
return Double.parseDouble(next()) ;
}
}
public class Main {
public static void main(String[] args) throws IOException {
int n = Reader.nextInt();
int L = Reader.nextInt();
int t = Reader.nextInt();
int[] s = new int[n];
int[] v = new int[n];
for(int i=0;i<n;i++) {
s[i] = Reader.nextInt();
v[i] = 1;
}
for(int i=0;i<t;i++) {
for(int j=0;j<n;j++) {
if((s[j]==0&&v[j]==-1)||(s[j]==L&&v[j]==1))
v[j]=v[j]*(-1);
s[j]=s[j]+v[j];
for(int k=0;k<n;k++) {
if(s[j]==s[k]&&j!=k) {
v[j]=v[j]*(-1);
v[k]=v[k]*(-1);
}
}
}
}
for(int i=0;i<n;i++)
System.out.print(s[i]+" ");
}
}