Please Help me. I want to make a program to solve tower of hanoi. But not using recursion and binary. Can you continue it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include<iostream>
using namespace std;
int main()
{
int disk, n=0, moves=1;
char tower;
cout<<"Enter the number of disks(3-9): ";
cin>>disk;
for(int k=0;k<disk;k++)
{
moves=moves*2;
}
for(int i=1;i<moves;i++)
{
if(i%2==1||i==1)
{
n=1;
}
else if(i%2==0)
{
if(i%4==0)
{
if(i%8==0)
{
if(i%16==0)
{
if(i%32==0)
{
if(i%64==0)
{
if(i%128==0)
{
if(i%256==0)
{
n=9;
}
}
else
{
n=7;
}
}
else
{
n=6;
}
}
else
{
n=5;
}
}
else
{
n=4;
}
}
else
{
n=3;
}
}
else
{
n=2;
}
}
cout<<"Move disk "<<n<<" to tower\n";
}
return 0;
}
|
Last edited on
Does it have to be without recursion?
Yes. But you can put a limit until nine disk only. help me please.
Last edited on