题解:AtCoder AT_awc0006_a Target Shooting Game
本文分享的必刷题目是从蓝桥云课、洛谷、AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。
欢迎大家订阅我的专栏:算法题解:C++与Python实现!
附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总
【题目来源】
AtCoder:A - 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 + WL+W.
一个靶子被放置在高桥前方距离L LL的位置。靶子具有一定的宽度,如果一个球落地点与高桥的距离至少为L − W L-WL−W且至多为L + W L+WL+W,则认为该球命中了靶子。
When Takahashi throws allN NNballs one by one, find the number of balls that hit the target.
当高桥依次扔出所有N NN个球时,求命中靶子的球数。
【输入】
N NNL LLW WW
D 1 D_1D1D 2 D_2D2… \ldots…D N D_NDN
- The 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【解题思路】
【算法标签】
#模拟#
【代码详解】
#include<bits/stdc++.h>usingnamespacestd;intn,l,w,ans;// n: 数据个数,l: 中心值,w: 允许的偏差,ans: 符合条件的数量intmain(){cin>>n>>l>>w;// 读入数据个数、中心值和允许偏差for(inti=1;i<=n;i++){intd;cin>>d;// 读入当前数据// 检查当前数据是否在[l-w, l+w]范围内if(d>=l-w&&d<=l+w){ans++;// 如果在范围内,计数器加1}}cout<<ans<<endl;// 输出符合条件的数量return0;}【运行结果】
5 10 3 5 8 10 13 15 3