模拟题。
#include <iostream>
#include <cstring>
using namespace std;
struct Ball{
int pos;
int d; //-1:left,1:right
Ball(){}
Ball(int a,int b):pos(a),d(b){}
}a[105]; //声明a需要无参构造函数
int s[1005];//0:no,1:one Ball,2:two Balls表示碰撞
int main(){
int n,L,t;
cin>>n>>L>>t;
memset(s,0,sizeof(s));
for(int i=0;i<n;i++){
int tmp; cin>>tmp;
a[i]=Ball(tmp,1);
s[tmp]=1;
}
for(int i=0;i<t;i++){
for(int j=0;j<n;j++){
Ball& ball=a[j];
if(ball.pos==0 && ball.d==-1){
s[0]-=1; s[1]+=1;
ball.d=1;
ball.pos=1;
}else if(ball.pos==L && ball.d==1){
ball.pos=L-1; ball.d=-1;
s[L]-=1; s[L-1]+=1;
}else{
s[ball.pos]-=1; //我是个智障吧,忘记更新s。。。
ball.pos+=ball.d;
s[ball.pos]+=1; //bug
}
}
for(int j=0;j<n;j++){ //可以这样判断,亦可以a[j-1],a[j+1]判断是否和a[j]碰撞
Ball& ball=a[j];
if(s[ball.pos]==2) ball.d=-ball.d;
}
}
for(int i=0;i<n;i++){ //直接cout<<a[i].pos<<" ";也给过,说明行的末尾可以有空格
if(i==n-1) cout<<a[i].pos;
else cout<<a[i].pos<<" ";
}
cout<<endl;
}
// 3 10 5
// 4 6 8
// 7 9 9