思路:

直接按照题意模拟,感觉没什么太好的办法。另外注意:int这种基础数据类型不能用equals这个方法 ,必须是Integer类型

import java.util.Scanner;
public class Main {
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;
}
}
sc.close();
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;
}
}

201803-2 碰撞的小球 Java的更多相关文章

  1. CCF认证201803-2 碰撞的小球 java代码实现。

    问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处.有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒. 当小球到达线 ...

  2. 【web前端学习部落22群】分享 碰撞的小球开源小案例

    对于课程中的疑问,大家可以加 web前端学习部落22群 120342833和其他老师还有众多的小伙伴们进行沟通交流哦,群里还有不少技术大拿.行业大牛 可以一起探讨问题,我们也会安排专业的技术老师为大家 ...

  3. 碰撞的小球 ccf (模拟)

    问题描述 试题编号: 201803-2 试题名称: 碰撞的小球 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐 ...

  4. 【C++】CCFCSP201803-2碰撞的小球

    // // main.cpp // CCFCSP20180318_2_碰撞的小球 // // Created by T.P on 2018/3/24. // Copyright © 2018年 T.P ...

  5. CCF CSP 201803-2 碰撞的小球

    题目链接:http://118.190.20.162/view.page?gpid=T72 问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处.有n个不计体积的小球在线段 ...

  6. CCF-201803-2 碰撞的小球

    问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处.有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒.当小球到达线段 ...

  7. ccf 201803-2 碰撞的小球(Python)

    问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处.有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒. 当小球到达线 ...

  8. CCF|碰撞的小球

    import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scan ...

  9. ZJNU 1422 - 碰撞的小球

    完全弹性碰撞可以视作互相穿过 所以直接考虑只有单个小球的时候,从板子上滑下所需要的时间即可 最后以30000为界分开流读入与缓冲区优化的io方法 //Case4用缓冲区io优化会WA??? /* Wr ...

随机推荐

  1. Problem A: Assembly Required K路归并

    Problem A: Assembly Required Princess Lucy broke her old reading lamp, and needs a new one. The cast ...

  2. Mybatis注解开发单表CRUD

    Mybatis注解开发单表CRUD mybatis注解开发和xml开发不可兼容,要么全部使用注解,要么全部使用xml,个人建议注解,简单. 当实体类属性名称和数据库表属性名称一致时:无需配置Resul ...

  3. CodeForces - 876D Sorting the Coins

    题意:有n个数的序列,n个数都为0,每次指定某个数变为1,当序列中第i个数为1,第i+1个数为0时,这两个数可交换,将序列从头到尾进行一次交换记为1次,直到某一次从头到尾的交换中没有任何两个数交换.序 ...

  4. PyCharm配置TensorFlow开发环境

    Anaconda自带的Jupyter Notebook很方便,但是执行速度较慢,缺少调试环境.PyCharm与Jupyter Notebook相比,执行速度更快,而且提供了类似Matlab的调试工具, ...

  5. express - 快速构建项目

    1,cnpm i express -g 2,  cnpm i express-generater -g 3, express - e  项目名

  6. flutter安装中的一些方法

    1.配置flutter环境变量 进入终端 vim ~/.bash_profile export ANDROID_HOME=~Library/Android/sdk export PATH=$PATH: ...

  7. setTimeout的异步

    http://www.cnblogs.com/littledu/articles/2607211.html http://www.cnblogs.com/rubylouvre/archive/2009 ...

  8. ROS常用库(一) fake_localization

    wiki是最好的学习资料,以下直接参考了wiki官网.另外po出官网网址,建议英语较好的朋友之接看原版 http://wiki.ros.org/fake_localization 概述 fake_lo ...

  9. jQuery通过name获取值

    使用jQuery获取name="day"的input对象: 方法1 var dayObj=$('input[name="day"]'); for(int i=0 ...

  10. idea下maven项目下spring junit 测试用例

    使用idea在编写的类下右键Go->Test或者ctrl+shift+t,点击create new test会在相应目录下创建test类 别写代码如下 @RunWith(value = Spri ...