challenge 漢数字で九九の表

漢数字で九九の表を作ってください。
ただし以下の条件をつけます。

条件
一.アラビア数字(0~9)禁止。
  プログラムにも出力結果にもアラビア数字を含んではいけない。(全角・半角とも)
二.結果の数字は、「七」とか「一○」(=10)とか「六四」(=64)のような形式とする。
三.九九の結果をそのままプログラム中に書き込んではいけない。

1
2
3
4
5
6
7
8
出力例

 一  二  三  ・・・・
 二  四  六  ・・・・
 三  六  九  ・・・・
 四  八 一二
 ・
 ・

Posted feedbacks - OCaml

組み込みの数値を一切使わないで書いてみました。わりと教科書的。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
type digit = | Zero | One | Two | Three | Four | Five
             | Six | Seven | Eight | Nine

let next_digit = function
    Zero -> One | One -> Two | Two -> Three | Three -> Four
  | Four -> Five | Five -> Six | Six -> Seven | Seven -> Eight
  | Eight -> Nine | Nine -> raise (Invalid_argument "next_digit, nine")
let prev_digit = function
    One -> Zero | Two -> One | Three -> Two | Four -> Three
  | Five -> Four | Six -> Five | Seven -> Six | Eight -> Seven
  | Nine -> Eight | Zero -> raise (Invalid_argument "prev_digit, zero")

let succ = function
  | (Nine, Nine) -> raise (Invalid_argument "succ, ninety-nine")
  | (x, Nine)    -> (next_digit x, Zero)
  | (x, y)       -> (x, next_digit y)
let pred = function
  | (Zero, Zero) -> raise (Invalid_argument "pred, zero")
  | (x, Zero)    -> (prev_digit x, Nine)
  | (x, y)       -> (x, prev_digit y)
let rec (@+) m n =
  match m with
    | (Zero, Zero) -> n
    | _ -> succ (pred m @+ n)
let rec (@*) m n =
  if m = (Zero, Zero) then (Zero, Zero)
  else n @+ (pred m @* n)

let string_of_digit = function
  | Zero  -> "〇" | One   -> "一" | Two   -> "二" | Three -> "三"
  | Four  -> "四" | Five  -> "五" | Six   -> "六" | Seven -> "七"
  | Eight -> "八" | Nine  -> "九"
let string_of_number = function
  | (Zero, y) -> "  " ^ string_of_digit y
  | (x, y)    -> string_of_digit x ^ string_of_digit y

let digit_list = [One; Two; Three; Four; Five; Six; Seven; Eight; Nine]
let _ =
  let nums = List.map (fun x -> (Zero, x)) digit_list in
  let products = List.map (fun m -> List.map (fun n -> m @* n) nums) nums in
  let prod_strings = List.map (List.map string_of_number) products in
  let rows = List.map (String.concat "  ") prod_strings in
    List.iter print_endline rows

Index

Feed

Other

Link

Pathtraq

loading...