---
title: "Ruby版本的“汉诺塔(Hanoi)”程序"
author: "koen"
date: "2009-09-29T13:20:20Z"
tags: []
slug: "ruby-hanoi"
draft: false
lastmod: "2009-09-29T13:20:20Z"
id: 480268
url: "https://koen.iteye.com/blog/480268"
---

```
def hanoi(n, from, to, temp)
  if(n<0)
    puts "n should be a positive integer."
  elsif(n==1)
    move(n, from, to)
  else
    hanoi(n-1, from, temp, to)
    move(n, from, to)
    hanoi(n-1, temp, to, from)
  end
end

def move(n, from, to)
  puts "Move No.#{n} disk from #{from} to #{to}."
end

hanoi(5, "Start", "Finish", "Temp")
```