1
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.

wslでelixir その127

Posted at

概要

wsl(wsl2じゃない)で、elixirやってみた。
LivebookでAtCoderやってみた。

写真

image.png

サンプルコード

abc245 d

defmodule Main do
	alias MyIO, as: IO
	def next_token(acc \\ "") do
		case IO.getn(:stdio, "", 1) do
			" " ->
				acc
			"\n" ->
				acc
			x ->
				next_token(acc <> x)
		end
	end
	def input(), do: IO.read(:line) |> String.trim()
	def ii(), do: next_token() |> String.to_integer()
	def li(), do: input() |> String.split(" ") |> Enum.map(&String.to_integer/1)

	defp output({result, _, _, _}), do: result |> Enum.join(" ")
	defp division(_, {acc, rem, cs, as}) do
		quo = div(hd(rem), hd(as))
		rem1 = Enum.zip(rem, as)
		|> Enum.map(fn {x, a} ->
			x - a * quo
		end)
		[c | cs1] = if Enum.empty?(cs), do: [nil], else: cs
		rem1 = (tl rem1) ++ [c]
		{[quo | acc], rem1, cs1, as}
	end
	defp solve(as, cs) do
		al = Enum.count(as)
		cl = Enum.count(cs)
		rem = Enum.take(cs, al)
		cs1 = Enum.drop(cs, al)
		1..cl - al + 1
		|> Enum.reduce({[], rem, cs1, as}, &division/2)
 		
		|> output()
    |> rev_integer_list()
    |> Enum.join(" ")

	end
	defp rev_integer_list(str) do
		str
		|> String.trim()
		|> String.split(" ")
		|> Enum.map(&String.to_integer/1)
		|> Enum.reverse()
	end
    
	def main() do
    li()
		as = li()
  	cs = li()
  	solve(as, cs)
		|> IO.puts()
	end
end

実行結果

{["1 2\n2 1\n12 14 8 2\n", "1 1\n100 1\n10000 0 -1\n"], ["6 4 2\n", "100 -1\n"]}
-- TEST 0 --
OK
-- TEST 1 --
OK

以上。

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