ccf 201803-2碰撞的小球

这篇文章描述了一个Java程序,使用Scanner输入创建了一个球类,模拟了多个球在给定长度的网格中按左右方向移动,并检测和处理球之间的碰撞。程序输出每个球最终的位置。
摘要由CSDN通过智能技术生成

import java.util.Scanner;
public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int L = sc.nextInt();
        int t = sc.nextInt();
        int timeCount = 0; //计时器
        Ball [] ball = new Ball[n];
        for (int i = 0; i < n; i++)
            ball[i] = new Ball(sc.nextInt(), 1);
        while (++timeCount <= t) {
            for (int i = 0; i < n; i++) {//到达端点则反向
                if(ball[i].direction == 1) {
                    ball[i].position++;
                    if(ball[i].position == L)
                        ball[i].direction *= -1; //使球反向
                } else {
                    ball[i].position--;
                    if(ball[i].position == 0)
                        ball[i].direction *= -1;
                }
            }
            for (int i = 0; i < n - 1; i++) //判断相撞
                for (int j = i + 1; j < n; j++)
                    if (ball[i].position.equals(ball[j].position)) {
                        ball[i].direction *= -1;
                        ball[j].direction *= -1;
                        break;
                    }
        }
        for(int i = 0; i < n; i ++) { //输出结果
            System.out.print(ball[i].position + " ");
        }
    }
}
class Ball {
    public Integer position; //球的位置
    public Integer direction; //球的方向,1为向右,-1为向左
    public Ball(Integer position, Integer direction) {
        this.position = position;
        this.direction = direction;
    }
}

另一种解法(小球之间的碰撞)

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
//3 10 5
//        4 6 8
class Ball {
    int position;
    int direction;//1--向右,-1--向左

    public Ball(int position, int direction) {
        this.position = position;
        this.direction = direction;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int L = scanner.nextInt();
        int time = scanner.nextInt();
        Ball[] balls = new Ball[n];
        for(int i = 0; i < n;i++){
            int pos = scanner.nextInt();
            balls[i] = new Ball(pos, 1);
        }

        for(int t = 0; t < time; t++){

            for(int i = 0; i < n; i++){
                if(balls[i].direction == 1){
                    balls[i].position++;
                    if(balls[i].position == L){
                        balls[i].direction *= -1;
                    }
                }
                else{
                    balls[i].position--;
                    if(balls[i].position == 0){
                        balls[i].direction *= -1;
                    }
                }
            }

            Map<Integer, Ball> map = new HashMap<>();
            for(int i = 0; i < n; i++){
                if(!map.containsKey(balls[i].position)){
                    map.put(balls[i].position, balls[i]);
                }
                else{
                    balls[i].direction *= -1;
                    map.get(balls[i].position).direction *= -1;
                }
            }
        }

        for(int i = 0; i < n; i++){
            System.out.print(balls[i].position+ " ");
        }

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值