0
我想解決這個問題:http://code.google.com/codejam/contest/351101/dashboard#s=p0使用F#。F#Store Store的解決方案
我是新來的函數式編程和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值而不是在一個平常的物品命令性的方式 - 我想要功能性的方法。
它確實是我一直在尋找的!看起來功能強大且易於閱讀。你能否給我一個完整的解決方案..? –
如果你描述什麼是你的(甚至是勢在必行)算法,我可以試一試。 – pad
我已經更新了代碼。它解決了這個問題,但沒有包含我想要的功能方法(用於學習目的)。 –