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 で 競プロでのデバッグを標準エラー出力で行っているという話 やってみた

Posted at

はじめに

@amaguri0408 さんの記事 競プロでのデバッグを標準エラー出力で行っているという話 が素敵でしたので、やってみました。

Ruby(WA)

N, X = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
known = [false] * N

x = X
while known[x].!
  known[x] = true
  x = A[x]
end
ans = 0
known.each do |i|
  ans += 1 if i
end
puts ans
4 2
3 1 1 2
2

うむ、3 が正答なのでWAですね。

p

N, X = gets.split.map(&:to_i)
A = gets.split.map{ _1.to_i - 1 }
known = [false] * N

x = X - 1
while known[x].!
  known[x] = true
  x = A[x]
  p [x, known]
end
ans = 0
known.each do |i|
  ans += 1 if i
end
puts ans
4 2
3 1 1 2
[0, [false, true, false, false]]
[2, [true, true, false, false]] 
[0, [true, true, true, false]]  
3

Rubyでデバッグするときはpを使います。

warn

N, X = gets.split.map(&:to_i)
A = gets.split.map{ _1.to_i - 1 }
known = [false] * N

x = X - 1
while known[x].!
  known[x] = true
  x = A[x]
  warn "#{x} #{known}"
end
ans = 0
known.each do |i|
  ans += 1 if i
end
puts ans

20211206a.png
おぉ、標準エラー出力にキターーーーーー

まとめ

  • @amaguri0408 さん、ありがとう
  • AtCoderに詳しくなった
2
0
0

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?