cff 2018.03-02碰撞的小球

2018.03-02碰撞的小球(100分)

(小声说一句 就不吃南瓜02 也是我哒 自己抄自己哒)

问题描述:
数轴上有一条长度为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
1
2
输出样例:

7 9 9
1
样例输入

10 22 30
14 12 16 6 10 2 8 20 18 4
1
2
样例输出

6 6 8 2 4 0 4 12 10 2
1
样例说明:

初始时,三个小球的位置分别为4, 6, 8。

一秒后,三个小球的位置分别为5, 7, 9。   
两秒后,第三个小球碰到墙壁,速度反向,三个小球位置分别为6, 8, 10。   
三秒后,第二个小球与第三个小球在位置9发生碰撞,速度反向(注意碰撞位置不一定为偶数),三个小球位置分别为7, 9, 9。   
四秒后,第一个小球与第二个小球在位置8发生碰撞,速度反向,第三个小球碰到墙壁,速度反向,三个小球位置分别为8, 8, 10。   
五秒后,三个小球的位置分别为7, 9, 9。
数据规模和约定:
对于所有评测用例,1 ≤ n ≤ 100,1 ≤ t ≤ 100,2 ≤ L ≤ 1000,0 < ai < L。L为偶数。

保证所有小球的初始位置互不相同且均为偶数。

在此我必须说两句 PTA上弥补了ccf的漏洞 标程可以过但是更加注重细节 所以有两个版本

代码详情(1.0)

#include<iostream>

using namespace std;

int main() {
	int n, L, t;
	cin >> n >> L >> t;
	//location用来存储小球的位置,
	//direction用来表示小球运动的方向(1表示向右,-1表示向左)
	int location[102];
	int direction[102];

	//初始化方向为1(右)
	for (int i = 0; i < n;i++) {
		direction[i] = 1;
		cin >> location[i];
	}
	//小球碰撞只发生在整数时刻
	
	for (int time = 0; time < t; time++) {//用来跑秒

		for (int i = 0; i < n; i++) {
			
			if (location[i] == L) {//右端点
				direction[i] = -1;
			}else if (location[i] == 0&&time!=0) {//左端点
				direction[i] = 1;
			}
			location[i] += direction[i];
			//判断是否相撞
			//只有不同方向才可能碰撞,只需要处理撞后反向
			//不在需要别的检查条件 反正下一秒就刷新了
			for (int j = 0; j < n&& i != j; j++) {
				//碰撞时 i和j不相等代表不同的小球
				//否则第i个小球都至少与第j(当j==i)个小球的位置相等
				if (location[i] == location[j] ) {
					direction[i] *= (-1);
					direction[j] *= (-1);
					//这一秒查出碰撞 下一秒刷新
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		cout << location[i] << " ";
	}
	cout << endl;
	return 0;
}

代码详情(2.0)

#include<iostream>
#include<algorithm>
using namespace std;
class Ball
{
public:
	int direction;
	int location;
	int num;//给定初始顺序 最后还得排回来
};
bool SortLocate (Ball a, Ball b)
{
	return a.location < b.location;
	//按照位置排序 碰撞只发生在相邻位置
}
bool SortNum(Ball a, Ball b)
{
	return a.num < b.num;
}

Ball a[105];
int main()
{
	int n, L, t;
	cin >> n >> L >> t;
	for (int i = 0; i < n; i++)
	{
		a[i].num = i;
		cin >> a[i].location;
		a[i].direction = 1;
		if (a[i].location == L)
			a[i].direction = -a[i].direction;//起步时先摆正方向
	}
	sort(a, a + n, SortLocate);
	int k = 0;
	while (k < t)
	{
		k++;
		for (int i = 0; i < n; i++)
		{
			a[i].location += a[i].direction;
			if (a[i].location == 0 || a[i].location == L)
				a[i].direction = -a[i].direction;
		}
		for (int j = 1; j < n; j++)
		{
			if (a[j].location == a[j - 1].location)
			{
				a[j].direction = -a[j].direction;
				a[j - 1].direction = -a[j - 1].direction;
			}//碰撞只发生在相邻位置 所以能降低复杂度
		}
	}
	sort(a, a + n, SortNum);
	for (int i = 0; i < n; i++)
	{
		cout << a[i].location << " ";
	}
	cout << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据你提供的安装命令,出现以下报错,请分析是什么原因,需要如何解决:C:\Users\Administrator>pip install pysqlcipher3 Collecting pysqlcipher3 Using cached pysqlcipher3-1.2.0.tar.gz (102 kB) Preparing metadata (setup.py) ... done Building wheels for collected packages: pysqlcipher3 Building wheel for pysqlcipher3 (setup.py) ... done WARNING: Legacy build of wheel for 'pysqlcipher3' created no files. Command arguments: 'C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe' -u -c ' exec(compile('"'"''"'"''"'"' # This is <pip-setuptools-caller> -- a caller that pip uses to run setup.py # # - It imports setuptools before invoking setup.py, to enable projects that directly # import from `distutils.core` to work with newer packaging standards. # - It provides a clear error message when setuptools is not installed. # - It sets `sys.argv[0]` to the underlying `setup.py`, when invoking `setup.py` so # setuptools doesn'"'"'t think the script is `-c`. This avoids the following warning: # manifest_maker: standard file '"'"'-c'"'"' not found". # - It generates a shim setup.py, for handling setup.cfg-only projects. import os, sys, tokenize try: import setuptools except ImportError as error: print( "ERROR: Can not execute `setup.py` since setuptools is not available in " "the build environment.", file=sys.stderr, ) sys.exit(1) __file__ = %r sys.argv[0] = __file__ if os.path.exists(__file__): filename = __file__ with tokenize.open(__file__) as f: setup_py_code = f.read() else: filename = "<auto-generated setuptools caller>" setup_py_code = "from setuptools import setup; setup()" exec(compile(setup_py_code, filename, "exec")) '"'"''"'"''"'"' % ('"'"'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-kpw5ylk5\\pysqlcipher3_64cff8baaca94d668d7efe41a1e57482\\setup.py'"'"',), "<pip-setuptools-caller>", "exec"))' bdist_wheel -d 'C:\Users\Administrator\AppData\Local\Temp\pip-wheel-kj2j7asn' Command output: [use --verbose to show] Running setup.py clean for pysqlcipher3 Failed to build pysqlcipher3 ERROR: Could not build wheels for pysqlcipher3, which is required to install pyproject.toml-based projects
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值