(算法练习)——201803-2碰撞的小球(CCF模拟)

这一题没有思路,因为严格考虑小球碰撞的话,会觉得很乱,无从下手= =
参考大佬的代码:
https://blog.csdn.net/SongBai1997/article/details/81175312
这题对于map的使用很巧妙,应该记下来,后面再遇到这种相对位移的题,都可以考虑下map
AC代码:

#include <stdio.h>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;

int main(){
	int n,L,t;
	int tmp[100],ans[100],a[100];
	map<int,int>dis;
	scanf("%d%d%d",&n,&L,&t);
	for(int i = 0;i <n;i++){
		scanf("%d",&a[i]);
		tmp[i] = a[i];
		int temp = a[i]+t;
		temp = temp%(2*L);
		
		if(temp>L){
			ans[i] = L- temp%L;
		}
		else{
			ans[i] = temp;
		}
	}
	sort(tmp,tmp+n);
	sort(ans,ans+n);
	for(int i = 0;i <n;i++){
		dis[tmp[i]] = i;
	}
	for(int i = 0;i <n;i++){
		printf("%d ",ans[dis[a[i]]]);
	}
}


/*
10 22 30
14 12 16 6 10 2 8 20 18 4
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值