Created
February 12, 2014 22:43
-
-
Save marwein/8966063 to your computer and use it in GitHub Desktop.
Tour de Hanoi iterative
This file contains 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
/* ************************************************************************** */ | |
/* */ | |
/* ::: :::::::: */ | |
/* hanoi_iterative.c :+: :+: :+: */ | |
/* +:+ +:+ +:+ */ | |
/* By: mbouallo <mbouallo@student.42.fr> +#+ +:+ +#+ */ | |
/* +#+#+#+#+#+ +#+ */ | |
/* Created: 2014/02/12 22:34:52 by mbouallo #+# #+# */ | |
/* Updated: 2014/02/12 22:39:13 by mbouallo ### ########.fr */ | |
/* */ | |
/* ************************************************************************** */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
void Hanoi(int n) | |
{ | |
int x; | |
char position1; | |
char position2; | |
for (x = 1; x < (1 << n); x++) | |
{ | |
switch((x & (x - 1)) % 3) | |
{ | |
case (0) : | |
position1 = 'A'; | |
break ; | |
case (1) : | |
position1 = 'B'; | |
break ; | |
case (2) : | |
position1 = 'C'; | |
break ; | |
default: | |
position1 = '0'; | |
} | |
switch (((x | (x - 1)) + 1) % 3) | |
{ | |
case (0) : | |
position2 = 'A'; | |
break ; | |
case (1) : | |
position2 = 'B'; | |
break ; | |
case (2) : | |
position2 = 'C'; | |
break ; | |
default: | |
position2 = '0'; | |
} | |
printf("%c -> %c\n", position1, position2); | |
} | |
} | |
int main() | |
{ | |
int n; | |
printf("Combien de Disques ? : "); | |
scanf("%d", &n); | |
printf("\n"); | |
Hanoi(n); | |
printf("\n"); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment