模拟小球运动

//模拟,排序//
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100;
struct Node
{
int id;//小球编号//
int pos;//位置//
int step;//移动的速度//
}b[N];
bool cmp1(Node a, Node b)
{
return a.pos < b.pos;a
}
bool cmp2(Node a, Node b)
{
return a.id < b.id ;
}
int main()
{
int n, l , t, id = 0;
cin >> n >> l >> t;
for(int i = 0 ; i < n ; i++)
{
b[i].id = ++id;
cin >> b[i].pos;
b[i].step = 1;
if(b[i].pos == l)
{
b[i].step = -b[i].step;
}
}
sort(b, b + n, cmp1);
for(int i = 0 ; i < t ; i++)
{
for(int j = 0 ; j < n ; j++)
{
b[j].pos += b[j].step;
if(b[j].pos == l || b[j].pos == 0)
{
b[j].step = -b[j].step;
}
}
for(int j = 1 ; j < n ; j++)
{
for(j-1 ; j < n ; j++)
{
if(b[j - 1].pos == b[j].pos)
{
b[j - 1].step = -b[j - 1].step;
b[j].step = -b[j].step;
}
}
}
}
sort(b, b + n ,cmp2);
for(int i = 0 ; i < n ; i++)
{
cout << b[i].pos <<" ";
}
cout << endl;
return 0;
}

猜你喜欢

转载自www.cnblogs.com/zxc666/p/10356039.html