码迷,mamicode.com
首页 > 其他好文 > 详细

模拟小球运动

时间:2019-02-08 13:17:39      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:cout   速度   ==   ++   std   位置   step   main   编号   

//模拟,排序//
#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;
}

模拟小球运动

标签:cout   速度   ==   ++   std   位置   step   main   编号   

原文地址:https://www.cnblogs.com/zxc666/p/10356039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!