結果

問題 No.49 算数の宿題
ユーザー eseharaesehara
提出日時 2021-06-10 18:14:30
言語 OCaml
(5.1.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,197 bytes
コンパイル時間 1,995 ms
コンパイル使用メモリ 18,488 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-24 17:50:03
合計ジャッジ時間 2,660 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,388 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

let test_text = "2497*3+4";;
let list_of_string x = String.to_seq x |> List.of_seq;;
let test_chars = list_of_string test_text;;
let int_of_list x = List.to_seq x |> String.of_seq |> int_of_string;;
let list_of_int x = string_of_int x |> String.to_seq |> List.of_seq;;

let init_value = [[]];;
let calc lst =
  let first = (List.nth lst 0 |> List.rev |> int_of_list)  in
  let op = List.nth lst 1 |> List.hd in
  let second = (List.nth lst 2 |> int_of_list) in
  match op with
  | '+' -> first * second
  | '*' -> first + second
  | _ -> failwith "ParseError: op is not '+' or '*'";;

let rec solve chars work =
  match chars with
  | [] -> calc work
  | hd::tl ->
    match hd with
    | '*' | '+' ->
      begin
        match List.length work with
        | 1 -> solve tl [[]; [hd]; List.hd work |> List.rev]
        | 3 -> solve tl [[]; [hd]; calc work |> list_of_int]
        | _ -> failwith "solve error: when op "
      end
    | _ ->
      let workhd = List.hd work in
      let worktl = List.tl work in
      let workhd = hd::workhd in
      solve tl (workhd :: worktl);;

let () =
  let chars = read_line () |> list_of_string in
  solve chars init_value |> print_int;
  print_newline ();
0