我想解决这个练习:http ://code.google.com/codejam/contest/351101/dashboard#s=p0使用 F#。
我是函数式编程和 F# 的新手,但我非常喜欢这个概念和语言。我也喜欢 codejam 练习,它看起来很简单,但却很真实。有人可以指出我的解决方案吗?
目前我已经编写了这段代码,它只是简单的命令式,从功能的角度来看很难看:
(*
C - Credit
L - Items
I - List of Integer, wher P is single integer
How does the data look like inside file
N
[...
* Money
* Items in store
...]
*)
let lines = System.IO.File.ReadAllLines("../../../../data/A-small-practice.in")
let CBounds c = c >= 5 && c <= 1000
let PBounds p = p >= 1 && p <= 1000
let entries = int(lines.[0]) - 1
let mutable index = 1 (* First index is how many entries*)
let mutable case = 1
for i = 0 to entries do
let index = (i*3) + 1
let C = int(lines.[index])
let L = int(lines.[index+1])
let I = lines.[index+2]
let items = I.Split([|' '|]) |> Array.map int
// C must be the sum of some items
// Ugly imperative way which contains duplicates
let mutable nIndex = 0
for n in items do
nIndex <- nIndex + 1
let mutable mIndex = nIndex
for m in items.[nIndex..] do
mIndex <- mIndex + 1
if n + m = C then do
printfn "Case #%A: %A %A" case nIndex mIndex
case <- case + 1
我想找出加起来等于 C 值但不是以通常的命令方式的项目 - 我想要功能方法。