2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ruby と Python で解く AtCoder ARC 134 A

Last updated at Posted at 2022-01-29

はじめに

AtCoder Problems の Recommendation を利用して、過去の問題を解いています。
AtCoder さん、AtCoder Problems さん、ありがとうございます。

今回のお題

Difficulty: 247

いやー、この問題ちょっと鬼畜眼鏡ではないのかな。

Ruby

AC.rb
n, l, w = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
a << l
ans = (a[0] / w.to_r).ceil
n.times do |i|
  if a[i + 1] - a[i] - w > 0
    ans += ((a[i + 1] - a[i] - w) / w.to_r).ceil
  end
end
puts ans
WA.rb
n, l, w = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
a << l
ans = (a[0] / w.to_f).ceil
n.times do |i|
  if a[i + 1] - a[i] - w > 0
    ans += ((a[i + 1] - a[i] - w) / w.to_f).ceil
  end
end
puts ans

AC.rbWA.rbの違い、分かりますでしょうか。

Python

AC.py
from math import ceil
from fractions import Fraction

n, l, w = map(int, input().split())
a = list(map(int, input().split()))
a.append(l)
ans = ceil(Fraction(a[0], w))
for i in range(n):
    if a[i + 1] - a[i] - w > 0:
        ans += ceil(Fraction(a[i + 1] - a[i] - w, w))
print(ans)
WA.py
from math import ceil

n, l, w = map(int, input().split())
a = list(map(int, input().split()))
a.append(l)
ans = ceil(a[0] / w)
for i in range(n):
    if a[i + 1] - a[i] - w > 0:
        ans += ceil((a[i + 1] - a[i] - w) / w)
print(ans)

Pythonだと書式が違いますので気付かれると思いますが、浮動小数の誤差による、ACWAの違いでした。

まとめ

  • ARC 134 A を解いた
2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?