题目信息:参考链接
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//碰撞的小球 0 - L
void ball(){
int n, L, t, i, x, y;
int *sign;
char *flag;
scanf("%d %d %d", &n, &L, &t);
flag = (char *)malloc(n);
memset(flag, 0, n);
sign = (int *)malloc(n * sizeof(int));
for(i = 0; i < n; i++){
scanf("%d", &sign[i]);
}
while(t){
for(i = 0; i < n; i++){
if(flag[i] == 0){//右移
if(sign[i] == L){ //右端点
flag[i] = 1;
sign[i]--;
}
else{
sign[i]++;
}
}
else{//左移
if(sign[i] == 0){ //左端点
flag[i] = 0;
sign[i]++;
}
else{
sign[i]--;
}
}
}
//碰撞检测
for(x = 0; x < n - 1; x++){
for(y = x + 1; y < n; y++){
if(sign[x] == sign[y]){
if(flag[x] == 0){
flag[x] = 1;
flag[y] = 0;
}
else{
flag[y] = 1;
flag[x] = 0;
}
}
}
}
t--;
}
for(i = 0; i < n; i++){
printf("%d ", sign[i]);
}
}
int main(){
ball();
return 0;
}
来源:CSDN
作者:SpiderPi
链接:https://blog.csdn.net/weixin_39274015/article/details/104580767