forked from Aniket965/Hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Aniket965#4141 from Maxlobo/master
Merged by aniket965
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
//Declarando Função | ||
void hanoi(int, char, char, char); | ||
int movimentos(int); | ||
|
||
//======================================================================== | ||
int main(int argc, char *argv[]) | ||
{ | ||
int discos; | ||
|
||
printf("Forneca o numero de discos: \n"); | ||
scanf("%d", &discos); | ||
if (discos < 1) | ||
{ | ||
while (discos < 1) | ||
{ | ||
printf("Valor invalido. Insira novamente: \n"); | ||
scanf("%d", &discos); | ||
} | ||
} | ||
printf("numero de movimentos: %d\n", movimentos(discos)); | ||
hanoi(discos, 'A', 'C', 'B'); | ||
return 0; | ||
} | ||
//========================================================================= | ||
void hanoi(int d, char ori, char dest, char aux) | ||
{ | ||
if (d == 1) | ||
{ | ||
printf("Mover disco %d de %c para %c\n", d, ori, dest); | ||
} | ||
else | ||
{ | ||
hanoi(d - 1, ori, aux, dest); | ||
printf("Mover disco %d de %c para %c\n", d, ori, dest); | ||
hanoi(d - 1, aux, dest, ori); | ||
} | ||
} | ||
|
||
int movimentos(int discos) | ||
{ | ||
if (discos == 1) | ||
{ | ||
return 1; | ||
} | ||
else | ||
{ | ||
return 2 * movimentos(discos - 1) + 1; | ||
} | ||
} |