递归与迭代

1.Fibonacci (递归法)


1) {
		return (fac($n-1) + fac($n-2));
	}
	
}
echo $fac = fac($n);
?>


  斐波那契(迭代法)

  参考:http://blog.csdn.net/a9529lty/article/details/4564758



2.阶乘(递归法)

  阶乘(迭代法)





3.hanoi 塔

递归与迭代_第1张图片

参考:http://www.luocong.com/dsaanotes/index-Z-H-8.htm#node_sec_7.2.3

           http://zh.wikipedia.org/wiki/%E6%B1%89%E8%AF%BA%E5%A1%94

 $destination";
		echo "
"; } else{ hanoi($n-1, $source,$destination,$auxiliary); echo "Move disk $n $source --> $destination"; echo "
"; hanoi($n-1, $auxiliary,$source,$destination); } } hanoi($n, $source, $auxiliary, $destination);

Tower of Hanoi 迭代法(还不会现在)

note:1How does this iterative Tower of Hanoi work? C [duplicate]

          2.http://phoxis.org/2012/05/01/towers-of-hanoi-iterative-process-simulating-recursion/
 


4.杨辉三角(pascal's triangle)



 
  

";
}

function print_triangle($row,$col) {
	if($col == 1 || $col == $row){
		return 1;
	}
	else{
		return print_triangle($row-1, $col-1)+print_triangle($row-1, $col);
	}
}
?>



你可能感兴趣的:(php,ACM)