Files
python/TangDou/XiTi/1343.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

40 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
解题的基本思路是用动态数组B,V分别保存小球的位置和速度方向然后一秒一秒的确定小球位置,
小球先判别是否在线段两头然后移动接下来在判别小球间是否碰撞,如果发生碰撞则双方速度方向改变
*/
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, L, t;//n小球个数,L线段长度,t时间
cin >> n >> L >> t;
int *B = new int[n];//用来记录n个小球的位置
int *V = new int[n];//用来记录小球的运动方向1代表右,-1代表左
for (int i = 0; i < n; i++) {//初始化
cin >> B[i];
V[i] = 1;
}
//一秒一秒的确定小球的位置及运动方向
while (t--) {
for (int i = 0; i < n; i++) {
if (B[i] == 0 || B[i] == L) {
V[i] = -V[i];
}
B[i] = B[i] + V[i];
}
//如果小球碰撞则速度方向改变
for (int i = 0; i < n; i++) {
for (int j = 0; j < n && j != i; j++) {
if (B[i] == B[j]) {
V[i] = -V[i];
V[j] = -V[j];
}
}
}
}
for (int i = 0; i < n; i++) {
cout << B[i] << " ";
}
}