Created
June 26, 2013 01:53
-
-
Save rossomax/5864154 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main; | |
| import ( | |
| "fmt" | |
| ) | |
| var memo = [][]int {}; | |
| func init() { | |
| memo = make([][]int, 100000); | |
| for i := 0; i < 100; i++ { | |
| memo[i] = make([]int, 100000); | |
| } | |
| } | |
| func ackermann(m, n int) int { | |
| res := 0; | |
| v := memo[m][n]; | |
| if (v != 0) { | |
| res = v; | |
| } else { | |
| switch { | |
| case m == 0: | |
| res = n + 1; | |
| case n == 0: | |
| res = ackermann(m - 1, 1); | |
| default: | |
| res = ackermann(m - 1, ackermann(m, n - 1)); | |
| } | |
| memo[m][n] = res; | |
| } | |
| return res; | |
| } | |
| func main() { | |
| fmt.Printf("ackermann(2, 1) = %d\n", ackermann(2, 1)); | |
| fmt.Printf("ackermann(2, 2) = %d\n", ackermann(2, 2)); | |
| fmt.Printf("ackermann(3, 4) = %d\n", ackermann(3, 4)); | |
| fmt.Printf("ackermann(4, 1) = %d\n", ackermann(4, 1)); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CodeIQ アッカーマンの呪い
2次元配列のスマートな初期化の方法を考えるのが面倒臭かったので、とりあえず力技で。