Skip to content

Instantly share code, notes, and snippets.

@leorcvargas
Created July 25, 2018 23:45
Show Gist options
  • Save leorcvargas/d0885edcd0d54b635b71d5185fe77fe6 to your computer and use it in GitHub Desktop.
Save leorcvargas/d0885edcd0d54b635b71d5185fe77fe6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void hanoi(int disco, char orig, char dest, char aux) {
if (disco == 1) {
printf("Move disco %d de %c para %c\n", disco, orig, dest);
} else {
hanoi(disco - 1, orig, aux, dest);
printf("Move disco %d de %c para %c\n", disco, orig, dest);
hanoi(disco - 1, aux, dest, orig);
}
}
int main(void) {
setbuf(stdout, NULL);
printf("** Torre de Hanoi **\n");
int n;
char sair;
do {
do {
printf("\nDigite o numero de discos: ");
fflush(stdin);
scanf("%d", &n);
if (n > 10)
printf("\nLimite de discos excedido!(10)\n");
} while (n > 10);
printf("\n");
hanoi(n, 'A', 'B', 'C');
printf("\nNumero de movimentos otimos: %.0f\n\n", pow(2, n) - 1);
printf("Deseja sair? (s/n): ");
fflush(stdin);
scanf("%c", &sair);
} while (sair == 'n');
printf("\nVoce saiu do programa.");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment