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?

paiza.ioでelixir その350

Posted at

概要

paiza.ioでelixirやってみた。
atcoder、見つけたので、やってみた。

参考にしたページ

練習問題

ABC 085 C - Otoshidama
10000円札と、5000円札と、1000円札が合計でN枚あって、合計金額がY円であった。このような条件を満たす各金額の札の枚数の組を1つ求める問題です。

投入するソース

9 45000

期待値

4 0 5 または 0 9 0

サンプルコード

defmodule Main do
	def solve_1(a, b, n, y) do
		if n >= 0 and 1000 * n == y do 
		    {a, b, n} 
		else 
		    nil 
		end
	end
	def solve_2(a, n, y) do
		0..(div(y, 5000)) 
		|> Enum.map(fn i -> 
		    solve_1(a, i, n - i, y - 5000 * i) 
		end) 
		|> List.foldl(nil, fn (x, y) -> 
		    if (is_nil(x)) do 
		        y 
		    else 
		        x 
		    end 
		end)
	end
	def solve_3(n, y) do
	   0..(div(y, 10000)) 
	   |> Enum.map(fn i -> 
	        solve_2(i, n - i, y - 10000 * i) 
	   end) 
	   |> List.foldl(nil, fn (x, y) -> 
	        if (is_nil(x)) do 
	            y 
	        else 
	            x 
	        end 
	   end)
	end
	def main do
		[n, y] = IO.gets("") 
		    |> String.trim() 
		    |> String.split(" ", trim: true) 
		    |> Enum.map(&String.to_integer(&1))
		ans = solve_3(n, y)
		if (is_nil(ans)) do
			IO.puts("-1 -1 -1")
		else
			{a, b, c} = ans
			IO.puts("#{a} #{b} #{c}")
		end
	end
end

実行結果

4 0 5

成果物

以上。

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