«前の日記(2010-10-24) 最新 次の日記(2010-10-26)» 編集

ToDo:

  • 99 深呼吸して、おちけつ (~12/31 あと250日)
  • 98 毎日アウトプットを重きにおいた目標をたてる (~12/31 あと250日)
  • 97 買物は行動を変えるもの・ことに限る (~12/31 あと250日)
  • 96 合理的な行動はときに我慢を強いる(~12/31 あと250日)

ɥozɐʞıɥ


2010-10-25 ハノイの塔 [長年日記]

_ [Lisp] ハノイの塔

Common Lisp

(defun hanoi(n a b c)

(cond ((> n 0)(hanoi (1- n) a c b)(print (format t "from ~A to ~A" a b))(hanoi (1- n) c b a))))

_ [Lisp] ハノイの塔

Scheme

(define (hanoi n a b c)

(if (> n 0)

(begin

(hanoi (- n 1) a c b)

(print (format "from ~A to ~A" a b))

(hanoi (- n 1) c b a))))

_ [C] ハノイの塔

#include <stdio.h>

#include <stdlib.h>

int main(int,char**);

void func(int,char*,char*,char*);

int main(int argc,char** argv){

if ( argc != 5 ){

printf ("error %d\n",argc);

return 1;

}

func(atoi(argv[1]),argv[2],argv[3],argv[4]);

return 0;

}

void func(int count,char* a,char* b,char* c){

if ( count > 0){

func(count -1,a,c,b);

printf ("from %s to %s\n",a,b);

func(count -1,c,b,a);

}

}

_ [Python] ハノイの塔

def hanoi(n,a,b,c):

if n > 0 :

hanoi((n-1),a,c,b)

print "from %s to %s" % (a, b)

hanoi((n-1),c,b,a)

if __name__ == "__main__":

hanoi( 3, "a", "b", "c")

_ [F#] ハノイの塔

let rec hanoi n pags =

if n > 0 then

match pags with

| p1::p2::p3::rest when rest.IsEmpty ->

hanoi (n - 1) (p1::p3::p2::rest)

printfn "%A -> %A" p1 p2

hanoi (n - 1) (p3::p2::p1::rest)

| _ -> failwith "3point needs"

hanoi 3 ["a";"b";"c"] |> ignore

_ [Clojure]ハノイの塔

(defn hanoi[n a b c]

(if (> n 0)

(do

(hanoi (dec n) a c b)

(println "from " a " to " b)

(recur (dec n) c b a))))

参考:algorithm - Towers of Hanoi with K pegs - Stack Overflow

_ 帰り際

このところずーっとLispをお勉強中です。再帰に関してはHanoiの塔のLogicのようにSimplifyして考えられるようになっておきたいと思いながら帰宅しました。

この証明ってムズいですね。帰納法になるのかな。



2002|09|10|11|12|
2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|
«前の日記(2010-10-24) 最新 次の日記(2010-10-26)» 編集