Atcoder beginner contest 198 C(初中数学)

2026年9月5日,又是一个平静的夜晚,第229场Atcoder常规赛正紧张进行着。我作为一名小菜鸡,只能观望,继续找以前初学者竞赛的题做一做,于是遇到了这题,让我回忆起了初中的一点数学知识,那我就记录一下吧。

Atcoder beginner contest 198
C - Compass Walking

Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 300 points

Problem Statement
Takahashi is standing at the origin of a two-dimensional plane. In one step, he can move to any point whose Euclidean distance from his current position is exactly R. (The coordinates of the destination need not be integers.) No other move is allowed.
Find the minimum number of steps needed for him to reach the point (X, Y).
The Euclidean distance between points (x1, y1) and (x2, y2) is sqrt((x1-x2)^2 + (y1-y2)^2).

Constraints

1 <= R <= 10^5
0 <= X, Y <= 10^5
(X, Y) != (0, 0)
All input values are integers.

Input
The input is given from Standard Input in the following format:

R X Y

Output
Print the minimum number of steps required.

Sample 1
Input:

5 15 0

Output:

3

He can reach in three steps: (0,0) -> (5,0) -> (10,0) -> (15,0). This is the minimum; he cannot do it in two or fewer steps.

Sample 2
Input:

5 11 0

Output:

3

One optimal path is: (0,0) -> (5,0) -> (8,4) -> (11,0).

Sample 3
Input:

3 4 4

Output:

2

One optimal path is: (0,0) -> (2 - sqrt(2)/2, 2 + sqrt(2)/2) -> (4,4).

此题是一道考查初中数学是否过关的题目。
分析:
1、当坐标(x,y)距离原点距离小于r时,一定存在一个可以迂回的点,经过这个点,在走2步的情况下到达(x,y)。
2、当坐标(x,y)距离原点距离等于r,直接返回1,也就是一步直达。
3、当坐标(x,y)距离原点距离大于r时,直接计算(x,y)到原点的距离,用这个距离除以r,然后向上取整就是答案。

Ruby解

r, x, y = gets.split.map(&:to_i)
if x ** 2 + y ** 2 == r ** 2
  puts 1
  exit
end
if x ** 2 + y ** 2 < r ** 2
  puts 2
  exit
end
z = (((x ** 2 + y ** 2) / (r ** 2).to_f).ceil.pow(0.5)).ceil
puts z
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容