3
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 5 years have passed since last update.

ElixirでABC165のA,B,C問題を制する !

Last updated at Posted at 2020-10-04

はじめに

問題

準備

  • Elixirをインストールしましょう
  • プロジェクトを作っておきます
$ mix new at_coder
$ cd at_coder

問題A - We Love Golf

  • 問題文はリンク先をご参照くださいませ :bow:
lib/abc_165_a.ex
defmodule Abc165A do
  def main() do
    k = IO.read(:line) |> String.trim() |> String.to_integer()

    [a, b] =
      IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)

    solve(k, a, b)
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc165/tasks/abc165_a

  ## Examples

      iex> Abc165A.solve(7, 500, 600)
      "OK"
      iex> Abc165A.solve(4, 5, 7)
      "NG"
      iex> Abc165A.solve(1, 11, 11)
      "OK"

  """
  def solve(k, a, b) do
    a..b
    |> Enum.any?(&(rem(&1, k) == 0))
    |> if(do: "OK", else: "NG")
  end
end


  • ## Examplesのところに書いてあるものは、Doctestsと呼ばれるものでしてテストができます
  • 解答のキモとなる関数について、問題に書いてある入力例をインプットして出力例の通りアウトプットされるかを確かめています
  • test/at_coder_test.exsに設定を足しておきましょう
test/at_coder_test.exs
defmodule AtCoderTest do
  use ExUnit.Case
  doctest Abc165A
$ mix test
..........

Finished in 0.2 seconds
9 doctests, 1 test, 0 failures
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:
  • 以下、B問題、C問題を解いていきます

問題B - 1%

lib/abc_165_b.ex
defmodule Abc165B do
  def main do
    IO.read(:line)
    |> String.trim()
    |> String.to_integer()
    |> solve()
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc165/tasks/abc165_b

  ## Examples

      iex> Abc165B.solve(103)
      3
      iex> Abc165B.solve(1000000000000000000)
      3760
      iex> Abc165B.solve(1333333333)
      1706

  """
  def solve(x) do
    1..3760
    |> Enum.reduce_while({0, 100}, fn year, {_, money} ->
      money = money + div(money, 100)
      {if(money >= x, do: :halt, else: :cont), {year, money}}
    end)
    |> elem(0)
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • 1題WA (Wrong Answer)がなかなかとれなかったのですが、他の方の回答を参考にして割り算で計算することで答えがAC (Accepted)が取れました
  • :tada::tada::tada:

問題C - Many Requirements

  • 問題文はリンク先をご参照くださいませ :bow:
lib/abc_165_c.ex
defmodule Abc165C do
  def main do
    [n, m, q] =
      IO.read(:line)
      |> String.trim()
      |> String.split(" ")
      |> Enum.map(&String.to_integer/1)

    1..q
    |> Enum.reduce([], fn _, acc ->
      list =
        IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)

      [list | acc]
    end)
    |> solve(n, m)
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc165/tasks/abc165_c

  ## Examples

      iex> Abc165C.solve([[1, 3, 3, 100], [1, 2, 2, 10], [2, 3, 2, 10]], 3, 4)
      110
      iex> Abc165C.solve([[2, 4, 1, 86568], [1, 4, 0, 90629], [2, 3, 0, 90310], [3, 4, 1, 29211], [3, 4, 3, 78537], [3, 4, 2, 8580], [1, 2, 1, 96263], [1, 4, 2, 2156], [1, 2, 0, 94325], [1, 4, 3, 94328]], 4, 6)
      357500
      iex> Abc165C.solve([[1, 10, 9, 1]], 10, 10)
      1

  """
  def solve(list_of_lists, n, m) do
    combinations(n, m)
    |> Enum.map(fn list ->
      list_of_lists
      |> Enum.reduce(0, fn [a, b, c, d], acc ->
        if Enum.at(list, b - 1) - Enum.at(list, a - 1) == c, do: acc + d, else: acc
      end)
    end)
    |> Enum.max()
  end

  def combinations(n, m) when n >= 2,
    do: do_combinations(n - 2, m, for(x <- 1..m, y <- x..m, do: [y, x]))

  defp do_combinations(0, _m, acc), do: acc |> Enum.map(&Enum.reverse/1)

  defp do_combinations(n, m, acc) do
    do_combinations(
      n - 1,
      m,
      for([head | _] = list <- acc, z <- head..m, do: [z | list])
    )
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:

Wrapping Up :qiita-fabicon:

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