Advertisement
sek1pa

rmq

Jan 26th, 2018
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.78 KB | None | 0 0
  1. -module(solution).
  2. -export([main/0]).
  3.  
  4. main() ->
  5.     {ok,[ArrLth,SCount]}=io:fread("","~d~d"),
  6.     Arr=read_row(),
  7.     List=lists:duplicate(math:pow(2,round(math:log2(ArrLth)))-ArrLth,0),
  8.     io:format("~w,~w~n",[lists:reverse(build_tree(Arr++List,[],[]))))]).
  9.  
  10. build_tree([],T,N) ->
  11.     case length(T)>1 of
  12.     true -> build_tree(lists:reverse(T),[],lists:reverse(T)++N);
  13.     false -> lists:reverse(T++N) end;
  14.  
  15. build_tree([A],T,N) -> build_tree([],[A|T],N);
  16. build_tree([A,B|Arr],T,N) ->
  17.     build_tree(Arr,[sum(A,B)|T],N).
  18.  
  19. read_row() ->
  20.     Line = io:get_line(""),
  21.     LineWithoutNL = string:strip(string:strip(Line, both, 10),both,13),
  22.     StrNumbers = re:split(LineWithoutNL, "\s+", [notempty]),
  23.     lists:map(fun erlang:binary_to_integer/1,StrNumbers).
  24.  
  25. sum(A,B) -> A+B.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement