Elixir로 ABC170의 A, B, C 문제를 제압한다!
소개
Elixir 에서 해 보았습니다
문제
준비
Elixir 설치
$ mix new at_coder
$ cd at_coder
문제 A - Five Variables

lib/abc_170_a.ex
defmodule Abc170A do
def main do
IO.read(:line)
|> String.trim()
|> String.split(" ")
|> Enum.map(&String.to_integer/1)
|> solve()
|> IO.puts()
end
@doc ~S"""
https://atcoder.jp/contests/abc170/tasks/abc170_b
## Examples
iex> Abc170A.solve([0, 2, 3, 4, 5])
1
iex> Abc170A.solve([1, 2, 0, 4, 5])
3
"""
def solve(list) do
Enum.find_index(list, &(&1 == 0)) + 1
end
end
## Examples
에 쓰여진 것은 Docteststest/at_coder_test.exs
에 설정을 더해 둡시다 test/at_coder_test.exs
defmodule AtCoderTest do
use ExUnit.Case
doctest Abc170A
$ mix test
..........
Finished in 0.2 seconds
9 doctests, 1 test, 0 failures
제출의 경우 모듈 이름은
Main
이어야합니다 


문제 B - Crane and Turtle
lib/abc_170_b.exdefmodule Abc170B do
def main do
[x, y] =
IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)
solve(x, y)
|> IO.puts()
end
@doc ~S"""
https://atcoder.jp/contests/abc170/tasks/abc170_b
## Examples
iex> Abc170B.solve(3, 8)
"Yes"
iex> Abc170B.solve(2, 100)
"No"
iex> Abc170B.solve(1, 2)
"Yes"
"""
def solve(x, y) when y - 2 * x >= 0 and x - div(y - 2 * x, 2) >= 0 and rem(y - 2 * x, 2) == 0,
do: "Yes"
def solve(_x, _y), do: "No"
end
defmodule Abc170B do
def main do
[x, y] =
IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)
solve(x, y)
|> IO.puts()
end
@doc ~S"""
https://atcoder.jp/contests/abc170/tasks/abc170_b
## Examples
iex> Abc170B.solve(3, 8)
"Yes"
iex> Abc170B.solve(2, 100)
"No"
iex> Abc170B.solve(1, 2)
"Yes"
"""
def solve(x, y) when y - 2 * x >= 0 and x - div(y - 2 * x, 2) >= 0 and rem(y - 2 * x, 2) == 0,
do: "Yes"
def solve(_x, _y), do: "No"
end
제출의 경우 모듈 이름은
Main
이어야합니다 


문제 C - Forbidden List

lib/abc_170_c.ex
defmodule Abc170C do
def main do
[x, n] =
IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)
if n > 0 do
IO.read(:line)
|> String.trim()
|> String.split(" ")
|> Enum.map(&String.to_integer/1)
|> solve(x)
|> IO.puts()
else
IO.read(:line)
solve([], x)
|> IO.puts()
end
end
@doc ~S"""
https://atcoder.jp/contests/abc170/tasks/abc170_c
## Examples
iex> Abc170C.solve([4, 7, 10, 6, 5], 6)
8
iex> Abc170C.solve([4, 7, 10, 6, 5], 10)
9
iex> Abc170C.solve([], 100)
100
"""
def solve([], x), do: x
def solve(list, x) do
0..100
|> Enum.reduce_while(x, fn i, acc ->
minus_one = x - i
if !(minus_one in list) do
{:halt, minus_one}
else
plus_one = x + i
if !(plus_one in list) do
{:halt, plus_one}
else
{:cont, acc}
end
end
end)
end
end
제출의 경우 모듈 이름은
Main
이어야합니다 


Wrapping Up




Reference
이 문제에 관하여(Elixir로 ABC170의 A, B, C 문제를 제압한다!), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/torifukukaiou/items/584d4e9b3e14792fb701텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)