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

ElixirでABC162のA,B,C問題を解いてみる!

Last updated at Posted at 2020-10-27

はじめに

  • Elixirでやってみました

問題

準備

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

問題A - Lucky 7

  • 問題文はリンク先をご参照くださいませ :bow:
defmodule Main do
  def main do
    IO.read(:line) |> String.trim() |> String.contains?("7") |> if(do: "Yes", else: "No") |> IO.puts
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:
  • この調子で以下、B問題、C問題を解いていきます

問題B - FizzBuzz Sum

defmodule Main do
  def main do
    n = IO.read(:line) |> String.trim() |> String.to_integer()
    1..n |> Enum.reduce(0, fn i, acc -> if rem(i,3) == 0 or rem(i,5) == 0, do: acc, else: acc + i end) |> IO.puts()
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:

問題C - Sum of gcd of Tuples (Easy)

  • 問題文はリンク先をご参照くださいませ :bow:
defmodule Main do
  def main do
    k = IO.read(:line) |> String.trim() |> String.to_integer()
    list = for i <- 1..k, j <- 1..k, do: Integer.gcd(i, j)
    1..k |> Enum.flat_map(fn i -> Enum.map(list, & Integer.gcd(&1, i)) end) |> Enum.sum |> IO.puts
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • タイムアウトになるかなあと心配しましたがセーフ(1689 ms)でした
  • Integer.gcd/2は最大公約数(The greatest common divisor)を計算してくれます

Wrapping Up :qiita-fabicon:

  • A〜Cは自力でいけました! :smile:
  • 問題みてiexでちょっとためして行けそうだったのでMainモジュールをこさえて人間コンパイル? のみ通して直接提出してみました
    • それで今回はDoctestsは書いていません
      • 言い訳にはならない?
    • コンパイルエラーださずにAC取れました
  • Enjoy Elixir!!! :fire::rocket::rocket::rocket:
4
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
4
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?