1. 程式人生 > >CCF 2018/03/02 碰撞的小球

CCF 2018/03/02 碰撞的小球

題目

去官網看

思路

  1. 每個時刻檢查是否有小球到達了端點,端點處逆向。
  2. 每個時刻檢查是否有兩個小球到達了同一點,碰撞時逆向

C++ 程式碼

#include <iostream>
using namespace std;
int main()
{
	int n, l, t;
	int r1[100], r2[100];
	cin >> n >> l >> t;
	for (int i = 0; i < n; ++i)
	{
		cin >> r1[i];
	}
	for (int i = 0; i < n; ++i)
	{
		r2[i] = 1;
	}
	for (int i = 0; i < t; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			if (r1[j] == 0||r1[j] == l)
			{
				r2[j] *= -1;
			}
			for (int k = 0; k < n; ++k)
			{
				if (r1[j] == r1[k] && j != k)
				{
					r2[j] *= -1;
					r2[k] *= -1;
				}
			}
			r1[j] += r2[j];
		}
	}
	for (int i = 0; i < n; ++i)
	{
		cout << r1[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

參考

https://blog.csdn.net/qq_35445741/article/details/79966281