Problem 181
http://projecteuler.net/index.php?section=problems&id=181
import Data.Array (range) import Data.Array.IO (IOArray,newArray,readArray,writeArray) import Control.Monad (forM_) b = 60;w = 40 main = do g <- newArray ((0,0),(b,w)) 0 :: IO (IOArray (Int,Int) Integer) writeArray g (0,0) 1 forM_ [(i,j,k,l) | (i,j) <- tail.range $ ((0,0),(b,w)), (k,l) <- range((i,j),(b,w))] $ \(i,j,k,l) -> readArray g ((k-i),(l-j)) >>= \t -> readArray g (k,l) >>= writeArray g (k,l).(t+) print =<< readArray g (b,w)
多分javaのほうが可読性が高い。
public class P181{ public static void main(String[] args){ int b = 60, w = 40; long[][] g = new long [b+1][w+1]; for(int i=0;i<=b;i++)for(int j=0;j<=w;j++){ if(i==0&&j==0) {g[0][0]=1;continue;} for(int k=i;k<=b;k++)for(int l=j;l<=w;l++)g[k][l]+=g[k-i][l-j]; } System.out.println(g[b][w]); } }