问题描述
  数轴上有一条长度为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

思路如下:

每个小球是一个类,分别含有三个属性:方向,位置,编号。然后根据时间的推移对小球的位置进行判断和推移。对小球的属性进行更改。小球存储在list列表中,因为arraylist的查找性能比较强并且有序,将小球信息存储在arraylist中。最后输出球的位置信息。贴一下java代码,欢迎各位大佬批评指正。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class BallMotion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ballCount = sc.nextInt();
int lineLen = sc.nextInt();
int time = sc.nextInt();
List<Ball> ballList = new ArrayList<Ball>();
for (int i = 1; i <= ballCount; i++) {
Ball ball = new Ball(i,1,sc.nextInt());
ballList.add(ball);
}
for(int i = 1; i <= time;i++){
//开始时间循环
for(int j = 0; j< ballList.size(); j++){
//是否在尽头
if(ballList.get(j).getPosition() == lineLen || ballList.get(j).getPosition() == 1){
ballList.get(j).setDirction(-ballList.get(j).getDirection());
}
//是否有球重合
for (int k = 0; k< ballList.size(); k++) {
if( k == j){
continue;
}else{
if (ballList.get(j).getPosition() == ballList.get(k).getPosition()) {
//重合时方向两个球的方向都相反
ballList.get(j).setDirction(-ballList.get(j).getDirection());
ballList.get(k).setDirction(-ballList.get(k).getDirection());
}
} }
// 移动位置
ballList.get(j).setPosition(ballList.get(j).getPosition()+ ballList.get(j).getDirection());
}
}
//打印各个小球位置
for(int i = 0; i < ballCount; i++){
System.out.print(ballList.get(i).getPosition()+" ");;
}
}
} class Ball {
private int id;// 编号
private int direction;// 方向,1右 -1左
private int position;// 位置 public Ball() {
} public Ball(int id, int direction, int position) {
this.id = id;
this.direction = direction;
this.position = position;
} public void setId(int id) {
this.id = id;
} public void setDirction(int direction) {
this.direction = direction;
} public void setPosition(int position) {
this.position = position;
} public int getId() {
return id;
} public int getDirection() {
return direction;
} public int getPosition() {
return position;
} }

最近练习C++,再贴一把C++代码(思路还是没变,没想到过了这么久,思路还是一样的)

#include<iostream>
#include<list> using namespace std;
class ball {
public:
int id;
int direction;//1为右-1为左
int position;//当前位置
};
int main() {
int ballcount, lineLen, time;
cin >> ballcount >> lineLen >> time;
int pos;
list<ball> balist;
for (int i = ; i <= ballcount; i++) {
cin >> pos;
ball b;
b.id = i;
b.direction = ;
b.position = pos;
balist.push_back(b);
}
//开始计算球每个时刻的位置
list<ball>::iterator bit,tempbit;
for (int i = ; i < time; i++) {
for (bit = balist.begin(); bit != balist.end(); bit++) {
//判断是否在尽头
if (bit->position == lineLen || bit->position == ) {
bit->direction = -;
}
//判断是否有球重合
for (tempbit = balist.begin(); tempbit != balist.end(); tempbit++) {
if (bit->id == tempbit->id) {
continue;
}
else {
if (bit->position == tempbit->position) {
bit->direction = -bit->direction;
tempbit->direction = -bit->direction;
}
}
}
//移动球的位置
bit->position = bit->position + bit->direction;
}
}
//打印球的位置
for (bit = balist.begin(); bit != balist.end(); bit++) {
if (bit == balist.begin()) {
cout << bit->position;
}
else {
cout << " " << bit->position;
}
}
system("pause");
return ;
}

CCF认证201803-2 碰撞的小球 java代码实现。的更多相关文章

  1. 201803-2 碰撞的小球 Java

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

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

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

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

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

  4. ccf认证 201709-4 通信网络 java实现

    试题编号:                                                               201709-4 试题名称: 通信网络 时间限制: 1.0s 内 ...

  5. CCF认证历年试题

    CCF认证历年试题 不加索引整理会死星人orz 第一题: CCF201712-1 最小差值(100分) CCF201709-1 打酱油(100分) CCF201703-1 分蛋糕(100分) CCF2 ...

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

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

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

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

  8. 小明种苹果(续)第十七次CCF认证

    小明种苹果(续)第十七次CCF认证 题目 原题链接 ](http://118.190.20.162/view.page?gpid=T93) 很高心,在现在CCF CSP可以下载自己当时的答卷了,也就是 ...

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

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

随机推荐

  1. Maven学习总结(二):安装

    一:Maven下载 下载地址:http://maven.apache.org/download.cgi 下载完成后,得到一个压缩包,解压,可以看到maven的组成目录 Maven目录分析 bin:含有 ...

  2. CSS属性之relative

    0.相对定位relative特点 相对定位relative元素总是会占据位置,所占据的位置是在relative元素没有设置left/top/right/bottom属性时的位置: 相对定位relati ...

  3. is_array判断是否为数组

    if(is_array($arr)){ echo "是数组"; }else{ echo "不是数组"; }

  4. 给大家分享下坐标转换的代码的JS和Python两个版本的源码【转】

    /** * Created by Wandergis on 2015/7/8. * 提供了百度坐标(BD09).国测局坐标(火星坐标,GCJ02).和WGS84坐标系之间的转换 */ /** * 百度 ...

  5. Climbing Stairs 爬楼梯问题,每次可以走1或2步,爬上n层楼梯总方法 (变相fibonacci)

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  6. 下载 github 项目文件到本地方法

    下载 github 项目文件到本地方法 本篇终极,收集 3 种方法 最厉害 666 的方法 直接访问网站: 操作如下: 本地工具版下载方法 首先需要下载 git 客户端 我就不转载了,上面有客户端的使 ...

  7. unity震动效果

    using System.Collections; using System.Collections.Generic; using UnityEngine; //思想:在短时间内在规定圆内随机震动对象 ...

  8. Jmeter与LoadRunner的异同

    1.jmeter的架构跟loadrunner原理一样,都是通过中间代理,监控&收集并发客户端发现的指令,把他们生成脚本,再发送到应用服务器,再监控服务器反馈的结果的一个过程. 2.分布式中间代 ...

  9. SVG坐标系统和transformation彻底理解

    翻译自https://sarasoueidan.com/blog/svg-coordinate-systems/ SVG元素不像传统的HTML elements一样受制于css box model.这 ...

  10. 自动化测试基础篇--Selenium unittest生成测试报告(HTMLTestRunner)

    如何生成HTMLTestRunner测试报告.接上篇文章,对于unittest框架,运行后,测试结果不便于查看,同时多个case存在的时候,可能会导致case result记录不正确的情况. 为此,引 ...