题解:AtCoder AT_awc0006_a Target Shooting Game
本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。欢迎大家订阅我的专栏算法题解C与Python实现附上汇总贴算法竞赛备考冲刺必刷题C | 汇总【题目来源】AtCoderA - Target Shooting Game【题目描述】Takahashi is playing a target shooting game at a summer festival.高桥正在夏祭上玩一个射击游戏。In front of Takahashi,N NNballs are prepared. Each ball has a different weight, and the weight of thei ii-th ball isD i D_iDi. When Takahashi throws a ball, it flies a distance proportional to its weight. Specifically, when a ball of weightD DDis thrown, it flies exactly a distance ofD DD.在高桥面前准备了N NN个球。每个球的重量不同且第i ii个球的重量为D i D_iDi。当高桥扔出一个球时它会飞出一段与其重量成比例的距离。具体而言当一个重量为D DD的球被抛出时它恰好飞出距离D DD。A target is placed at distanceL LLfrom Takahashi. The target has some width, and a ball is considered to have hit the target if it lands at a distance from Takahashi that is at leastL − W L - WL−Wand at mostL W L WLW.一个靶子被放置在高桥前方距离L LL的位置。靶子具有一定的宽度如果一个球落地点与高桥的距离至少为L − W L-WL−W且至多为L W LWLW则认为该球命中了靶子。When Takahashi throws allN NNballs one by one, find the number of balls that hit the target.当高桥依次扔出所有N NN个球时求命中靶子的球数。【输入】N NNL LLW WWD 1 D_1D1D 2 D_2D2… \ldots…D N D_NDNThe first line containsN NNrepresenting the number of balls,L LLrepresenting the distance to the target, andW WWrepresenting the value related to the target’s width, separated by spaces.The second line containsD 1 , D 2 , … , D N D_1, D_2, \ldots, D_ND1,D2,…,DNrepresenting the weight of each ball, separated by spaces.【输出】Output the number of balls that hit the target in one line.【输入样例】5 10 3 5 8 10 13 15【输出样例】3【解题思路】【算法标签】#模拟#【代码详解】#includebits/stdc.husingnamespacestd;intn,l,w,ans;// n: 数据个数l: 中心值w: 允许的偏差ans: 符合条件的数量intmain(){cinnlw;// 读入数据个数、中心值和允许偏差for(inti1;in;i){intd;cind;// 读入当前数据// 检查当前数据是否在[l-w, lw]范围内if(dl-wdlw){ans;// 如果在范围内计数器加1}}coutansendl;// 输出符合条件的数量return0;}【运行结果】5 10 3 5 8 10 13 15 3