結果
| 問題 |
No.49 算数の宿題
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-06-10 18:14:30 |
| 言語 | OCaml (5.2.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,197 bytes |
| コンパイル時間 | 576 ms |
| コンパイル使用メモリ | 19,968 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-23 02:14:50 |
| 合計ジャッジ時間 | 1,167 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
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 ();