CCFCSP 201803-2碰撞的小球

tech2025-02-28  7

题目来源于CCF CSP



思路解析

根据碰撞的时候,会改变方向,那么我们将每一个小球的位置用数组存起来,遇到相同的位置且运动方向不同时,改变运动方向即可。


代码解析

//201803-2 碰撞的小球 #include<iostream> using namespace std; struct point { int pos; int direction; // -1<- ->1 }pos[100]; int main() { int n, L, t; cin >> n >> L >> t; for (int i = 0; i < n; ++i) { cin >> pos[i].pos; pos[i].direction = 1; } for (int tt = 0; tt < t; ++tt) { //先进行移动 for (int i = 0; i < n; ++i) { if (pos[i].direction == 1) { pos[i].pos += 1; if (pos[i].pos > L) { pos[i].pos = L - 1; pos[i].direction = -1; } } else if (pos[i].direction == -1) { pos[i].pos -= 1; if (pos[i].pos < 0) { pos[i].pos = 1; pos[i].direction = 1; } } } for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (pos[i].pos == pos[j].pos) { pos[i].direction = -pos[i].direction; pos[j].direction = -pos[j].direction; } } } } for (int i = 0; i < n; ++i) cout << pos[i].pos << " "; return 0; }

最新回复(0)