ccf 201803-2 碰撞的小球

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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct ball_info{
    int index;
    int position;
    int flag;
    ball_info(int _index, int _position, int _flag):index(_index), position(_position), flag(_flag){}
    bool operator < (const ball_info &t)const{
        return this->position< t.position;
    }
    bool operator == (const ball_info &t)const{
        return this->index==t.index;
    }
};
int n, l, t;
vector<ball_info> ball_v;
int main()
{
    cin>>n>>l>>t;
    for (int i = 0; i < n; ++i) {
        int position;
        cin>>position;
        ball_v.push_back(ball_info(i, position, 1));
    }
    sort(ball_v.begin(), ball_v.end());
    for (int i = 0; i < t; ++i) {
        //经过i时刻之后,小球状态
        for (int j = 0; j < n; ++j) {
            ball_v[j].position += ball_v[j].flag;
            if (ball_v[j].position==l || ball_v[j].position==0){
                ball_v[j].flag *= -1;
            }
        }

        for (int j= 0; j < n-1; ++j) {
            if(ball_v[j].position==ball_v[j+1].position) {
//                cout << "SAME" << ball_v[j].position << " " << ball_v[j + 1].position << "END" << endl;
                ball_v[j].flag *= -1;
                ball_v[j + 1].flag *= -1;
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        vector<ball_info>::iterator it = find(ball_v.begin(), ball_v.end(), ball_info(i, 0, 0));
        cout<< it->position<<" ";
    }

    return 0;
}

/*
10 22 30
14 12 16 6 10 2 8 20 18 4
*/
import java.util.*;
public class Main {
    public static Comparator<P> comp = new Comparator<P>() {
        @Override
        public int compare(P p, P t1) {
            return p.position<t1.position?-1:1;
        }
    };
    public static Comparator<P> comp1 = new Comparator<P>() {
        @Override
        public int compare(P p, P t1) {
            return p.index < t1.index?-1:1;
        }
    };
    public static void main(String[] args) {
        ArrayList<P> n_v = new ArrayList<P>();
        Scanner s = new Scanner(System.in);
        int n, l, t;
        n = s.nextInt();
        l = s.nextInt();
        t = s.nextInt();
        for(int i=0;i<n;i++){
            int position;
            position = s.nextInt();
            n_v.add(new P(i, position, 1));
        }
        Collections.sort(n_v, comp);
        for (int i=0;i<t;i++){
            for (int j=0;j<n;j++){
                n_v.get(j).position += n_v.get(j).flag;
                if(n_v.get(j).position==l || n_v.get(j).position==0)
                    n_v.get(j).flag *= -1;
            }
            for (int j = 0; j < n-1; j++) {
                if (n_v.get(j).position==n_v.get(j+1).position){
                    n_v.get(j).flag *= -1;
                    n_v.get(j+1).flag *= -1;
                }
            }
        }
        Collections.sort(n_v, comp1);
        for (int i = 0; i < n; i++) {
            System.out.print(n_v.get(i).position);
            System.out.print(" ");
        }
    }
}
class P{
    int index;
    int position;
    int flag;
    P(int _index, int _position, int _flag){
        this.index = _index;
        this.position = _position;
        this.flag = _flag;
     }
     @Override
     public boolean equals(Object o){
        P p_o = (P)o;
        if(p_o.index == this.index)
            return true;
        else return false;
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值