algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

main.ml (755B)


      1 let read_int () = 
      2     Scanf.scanf " %d" (fun x -> x)
      3  in
      4 
      5 let rec readInts size = 
      6         if size <> 0 then read_int () :: readInts (size - 1)
      7         else [] 
      8 in
      9 
     10 let largest arr =
     11     let rec acc best arr =
     12         match arr with
     13         | h :: t -> acc (if h > best then h else best) t
     14         | [] -> best
     15     in
     16     match arr with
     17     | [] -> failwith "Empty list"
     18     | h :: t -> acc h t
     19 in
     20 
     21 let rec handleTestCase num = 
     22     let size = read_int() in
     23         let arr = readInts size in
     24             let largest = largest arr in
     25                 let () = print_int (largest * size) in
     26                         let () = print_newline () in
     27                             if num > 1 then (handleTestCase (num - 1)) else ()
     28 in
     29 
     30 handleTestCase (read_int ())