直接模拟,时间复杂度 100 * 100 * 100 =10e6
No problem
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n, L, t;
struct Ball{
int pos, speed;
}p[N];
//判断小球是否相撞或到边界
bool knock(int k){
int pos = p[k].pos;
if(pos == 0 || pos == L) return true;
for(int i = 0; i < n; i ++){
if(i != k && pos == p[i].pos) return true;
}
return false;
}
int main()
{
cin >> n >> L >> t;
for (int i = 0; i < n; i ++ ){
int pos; cin >> pos;
p[i] = {pos, 1};
}
for (int i = 0; i < t; i ++ ){
//判断小球是否相撞或到边界
for (int j = 0; j < n; j ++ ){
if(knock(j)) p[j].speed *= -1;
}
//更新所有小球下一时刻的位置
for(int j = 0; j < n; j ++){
p[j].pos += p[j].speed;
}
}
for(int i = 0; i < n; i ++) cout << p[i].pos << ' ';
}