0
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でAtCoder ABC266(A, B, C)を解いてみた

Last updated at Posted at 2023-05-06

はじめに

Webエンジニアを目指して、RubyやRailsをいじってます。
今回は、RubyでAtCoder ABC266のA, B, Cを解きました。備忘録として解き方をまとめていきたいと思います。

A - Middle Letter

a-266.rb
s = gets.chomp
puts s[(s.size - 1) / 2]

B - Modulo Number

b-266.rb
puts gets.to_i % 998244353

解説

N-x=k*998244353(kは整数)より、x=N%998244353となります。

C - Convex Quadrilateral

c-266.rb
# 修正前
def cross(x, y, z)
  return (z[0] - y[0]) * (x[1] - y[1]) - (z[1] - y[1]) * (x[0] - y[0])
end

a, b, c, d = Array.new(4){ gets.split.map(&:to_i) }

[[a, b, c], [b, c, d], [c, d, a], [d, a, b]].each do |array|
  if cross(array[0], array[1], array[2]) <= 0
    puts "No"
    exit
  end
end
puts "Yes"
c-266.rb
# 修正後
require "matrix"

module CycleEachConsToArray
  refine Array do
    def cycle_each_cons(n)
      return Enumerator.new{ cycle_each_cons(n, &_1) } unless block_given?

      cycle.each_cons(n).take(length).each{ yield _1 }
    end
  end
end

using CycleEachConsToArray

a, b, c, d = Array.new(4){ gets.split.map(&:to_i) }
def coss(a, b, c, d)
  edges = [a, b, c, d].cycle_each_cons(2).map do |p1, p2|
    [p2[0] - p1[0], p2[1] - p1[1]]
  end
  return edges
end

coss(a, b, c, d).cycle_each_cons(2).each do |edge1, edge2|
  if Matrix[edge1, edge2].det <= 0
    puts "No"
    exit
  end
end
puts "Yes"

解説

(公式解説を参考にしました)
外積の値が0または負となるものは180度以上となります。

参考

0
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
0
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?