Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Alexandru Scvortov

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Tower of Hanoi

Solves the old "Tower of Hanoi" problem using a recursive divide et impera approach.

http://en.wikipedia.org/wiki/Tower_of_Hanoi

   1  
   2  #include <stdio.h>
   3  
   4  void hanoi(int n, char a, char b, char c) {
   5  	if (!n)	
   6  		return;
   7  	
   8  	hanoi(n - 1, a, c, b);
   9  	printf("%c -> %c\n", a, b);
  10  	hanoi(n - 1, c, b, a);
  11  }
  12  
  13  int main(int argc, char *argv[]) {
  14  	int n = 3;
  15  	hanoi(n, 'a', 'b', 'c');
  16  	
  17  	return 0;
  18  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS