我尝试添加由列表表示的数字,例如:
0 = []
1 = [1]
2005 = [2005]
123456 = [3456; 12]
1234567890 = [7890; 3456; 12]
1000000001 = [1; 0; 10]
所以add [9999] [1] = [0;1]
我已经这样做了:
let rec add l1 l2 =
match l1, l2 with
| n::l1', m::l2' -> if n+m < 10000 then
(n+m)::(add l1' l2')
else
begin
match l1', l2' with
| p::l1'', q::l2'' -> ((n+m) - 10000)::(add ((1+p)::l1'') l2')
| p::l1'', [] -> ((n+m) - 10000)::(add ((1+p)::l1'') l2')
| [], q::l2'' -> ((n+m) - 10000)::(add l1' ((1+q)::l2''))
| [], [] -> ((n+m) - 10000)::[1]
end
| [], [] -> []
| m::l1', [] -> m::(add l1' [])
| [], m::l2' -> m::(add [] l2')
但它不适用于类似于add [9999;9999] [1]
which give[0; 10000]
而不是[0;0;1]
.
随身携带有问题,但我找不到。
它适用于add [8500;6000] [5600;452]
.