c++小球在坐标上运动碰撞,计算t秒后位置

【问题描述】数轴上有一条长度为L (L为偶数)的线段,左端点在原点,石端点在坐标L处。有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒。当小球到达线段的端点(左端点或右端点)的时候,会立即向相反的方向移动,速度大小仍然为原来大小。当两个小球撞到一起的时候,两个小球会分别向与自己原来移动的方向相反的方向,以原来的速度大小继续移动。现在,告诉你线段的长度L,小球数量n,以及n个小球的初始位置,请你计算型之后,各个小球的位置。
【输入形式】输入的第一行包含三个整数n (1≤n ≤100), L (2≤L≤1000),t (1≤t≤100),用空格分隔,分别表示小球的个数、线段长度和你需要计算地之后小球的位置。第二行包含n个整数a1,a2,…,an,用空格分隔,表示初始时刻n个小球的位置。
【输出形式】输出一行包含八个整数,用空格分隔,第i个整数代表初始时刻位于ai的小球,在秒之后的位置。
【样例输入】
3 10 3
4 6 8
【样例输出】
7 9 9
【样例说明】初始时,三个小球的位置分别为4,6,8。一秒后,三个小球的位置分别为5,7,9。两秒后,第三个小球碰到墙壁,速度反向,三个小球位置分别为6,8,10。三秒后,第二个小球与第三个小球在位 置9发生碰撞,速度反向(注意碰撞位置不一定为偶数),三个小球位置分别为7,9,9。

提示:因为所有小球的初始位置都为偶数,而且线段的长度为偶数,可以证明,不会有三个小球同时相撞,小球到达线段端点以及小球之问的碰撞时刻均为整数。同时也可以证明两个小球发生碰撞的位置一定是整数(但不一定是偶数)。

https://blog.csdn.net/qq_15046309/article/details/82431331

#include <iostream>
using namespace std;

// Function to calculate the position of the balls after "t" seconds
void calculatePosition(int n, int L, int t, int* a)
{
    // loop through all the balls
    for (int i = 0; i < n; i++)
    {
        // calculate the new position of the ball
        int new_pos = a[i] + t;

        // if the new position is greater than the length of the line segment,
        // move the ball in the opposite direction
        if (new_pos > L)
            new_pos = L - (new_pos - L);

        // print the new position of the ball
        cout << new_pos << " ";
    }
}

int main()
{
    // Read the input
    int n, L, t;
    cin >> n >> L >> t;

    // Array to store the initial position of the balls
    int a[n];

    // Read the initial position of the balls
    for (int i = 0; i < n; i++)
        cin >> a[i];

    // Calculate and print the position of the balls after "t" seconds
    calculatePosition(n, L, t, a);

    return 0;
}

完整代码如下,望采纳


#include<cstdio>
#define maxn 1005
struct BALL
{
    int a;  //坐标
    int direction = 1;   //方向
}ball[maxn];

int toRight(int a)
{
    return ++a;
}
int toLift(int a)
{
    return --a;
}

int main()
{
    int n, L, t;
    scanf("%d%d%d", &n, &L, &t);
    for (int i = 0; i < n ; i++)
    {
        scanf("%d", &ball[i].a);
    }
    for(int i = 0;i < t; i++)
    {
        for(int j = 0; j < n ;j++)
        {
            if (ball[j].direction) ball[j].a = toRight(ball[j].a);
            else ball[j].a = toLift(ball[j].a);
        }
        for (int j = 0; j < n; j++)
        {
            if (ball[j].a == L) ball[j].direction = !ball[j].direction;  //碰到右边界向左
            if (ball[j].a == 0) ball[j].direction = !ball[j].direction;  //碰到左边界向右
            for (int k = j+1;k < n;k++)                //遍历小球,发现坐标相等记为碰撞将方向置反
            {
                if (ball[j].a == ball[k].a)
                {
                    ball[j].direction = !ball[j].direction;
                    ball[k].direction = !ball[k].direction;
                }
            }

        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("%d ", ball[i].a);
    }
    return 0;
}


#include <iostream>
using namespace std;
struct qiu{
    int site;                    //位置
    int v;                        //速度 
};
int main(){
    int n,L,t;                   //n为球的个数,L为长度(偶数),t为时间
    cin>>n>>L>>t; 
    int i=n,j;                        //循环变量 
    struct qiu a[n];            //球的结构数组  
    while(i--){                    //给每个球添加数据 
        cin>>a[i].site;
        a[i].v=1;                //初始化速度 
    }            
    while(t--){                    //时间段内球的数据变化,每次循环的时间段为一秒 
        for(i=0;i<n;i++){
            if(a[i].site==L||a[i].site==0) a[i].v*=-1;    //到达左右端点速度变成反向(以向右为正方向) 
            for(j=0;j<n;j++){
                if(a[i].site==a[j].site&&i!=j){            //site值相等且不是同一个球,速度变成反向 
                    a[i].v*=-1;
                    a[j].v*=-1;
                }
            }
        }
        for(i=0;i<n;i++) a[i].site+=a[i].v;            //这一秒的位置变化 
    }
    i=0;
    while(i!=n) cout<<a[i++].site<<" " ;
    return 0;
}

思路:由于小球碰撞后的运动方向发生了改变,因此可以设置一个标志方向的数组,小球向前走记为1,一开始先向前走,如果遇到了边缘,就改变标志方向的值为-1,然后与小球当前的位置相加,注意这个时候很容易犯错的一点,如果仅仅只是把当前的标志方向的值改为1,那么小球可能只是向相反方向走了一步,因此,应该把整个数组取相反数;
小球之间的碰撞,用两个for循环判断有没有位置相等的,有的话就把标志方向的数组取相反数就可以了
算法代价应该是n^3或者小一点,因为每一秒都要判断是否有位置相等的。

#include<bits/stdc++.h>
using namespace std;
int a[100];//记录小球的位置 
int b[100];//记录小球移动的方向 
int main()
{
    int n,L,t;
    cin>>n>>L>>t;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]=1;
    }
    for(int j=1;j<=t;j++)
    {
        for(int i=1;i<=n;i++)
        {
            a[i]=a[i]+b[i];
            if(a[i]==0||a[i]==L)
            {
                b[i]=-b[i];
             } 
             
        
        }
        for(int x=1;x<=n;x++)
             {
                 for(int y=x;y<=n;y++)
                 {
                     if(a[x]==a[y])
                     {
                         b[x]=-b[x];
                         b[y]=-b[y];
                     }
                 }
              } 
    }
    for(int i=1;i<=n;i++)
    {
        cout<<a[i]<<' ';
    }
 } 


试试
https://blog.csdn.net/weixin_51305111/article/details/128243309

https://blog.csdn.net/wenmiao_/article/details/82021370

碰撞的小球(c++


#include<iostream>
using namespace std;
int n,l,t;
struct ball{
    int posi;
    int speed;
};
bool ballcount(int num,ball balls[]){
    int countnum=0;
    for (int j=0;j<n;++j){
        if(balls[j].posi==num){
            countnum++;
            if(countnum>1){
                return true;
            }
        }
    }
    return false;
}
int main(){
    cin>>n>>l>>t;
    ball balls[n];
    for (int i=0;i<n;++i){
        ball ball1;
        cin>>ball1.posi;
        ball1.speed=1;
        balls[i]=ball1;
    }
    for (int i=0;i<t;++i){
        ball copyball[n];
        for (int k=0;k<n;++k){
            copyball[k]=balls[k];
        }
        for (int k=0;k<n;++k){
            if(balls[k].posi==l or balls[k].posi==0 or ballcount(balls[k].posi,balls)){
                copyball[k].speed=0-copyball[k].speed;
            }
            copyball[k].posi+=copyball[k].speed;
 
        }
        for (int k=0;k<n;++k){
            balls[k]=copyball[k];
        }
    }
    for (int i=0;i<n;++i){
        cout<<balls[i].posi<<" ";
    }
}

不知道你想要的是结果还是其他,这里提供一个类似题型的实例给你参考,供你理解,同时建议你阅读下评论区的评论,可增长你对该题的领悟,链接:https://tigerisland.blog.csdn.net/article/details/81660041?spm=1001.2101.3001.6650.17&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-17-81660041-blog-82431331.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-17-81660041-blog-82431331.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=24