201803-2碰撞的小球



// exercise.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include<iostream>
using namespace std;

struct ball {
	int pos;//位置
	int dir;//方向,1为右
};
ball balls[101];

int main()
{
	int n,L,t;
	cin >> n >> L >> t;
	//初始化
	for (int i = 0; i < n; i++)
	{
		cin >> balls[i].pos;
		balls[i].dir = 1;
	}
	//模拟运行
	int T = 0;
	while (++T <= t)
	{
		for (int i = 0; i < n; i++)
		{
			switch (balls[i].dir)
			{
			case 1:balls[i].pos += 1; break;
			case -1:balls[i].pos -= 1; break;
			default:break;
			}
		}
		for (int i = 0; i < n; i++)
		{
			if (balls[i].pos == L)//撞墙,若撞则不可能碰撞
			{
				balls[i].dir = -1;
			}
			else if(balls[i].pos==0)
			{
				balls[i].dir = 1;
			}
			else
			{
				for (int j = i + 1; j < n; j++)
				{
					if (balls[i].pos == balls[j].pos)//碰撞
					{
						balls[i].dir = -balls[i].dir;
						balls[j].dir = -balls[j].dir;
					}
				}
			}

		}
	}

	for (int i = 0; i < n; i++)
	{
		cout << balls[i].pos << " ";
	}

	return 0;
}
posted @ 2019-08-16 18:21  归根复命  阅读(117)  评论(0编辑  收藏  举报