#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void towers(int,char,char,char);
void towers(int n,char frompeg,char topeg,char auxpeg)
{
if(n==1)
{ printf("\n move disk 1 from peg %c to peg %c", frompeg,topeg);
return;
}
towers(n-1,frompeg,auxpeg,topeg);
printf("\n move disk %d from %c to peg %c",n,frompeg,topeg);
towers(n-1,auxpeg,topeg,frompeg);
}
int main()
{
int n;
time_t start,end;
double timetaken;
printf("enter the no of disks:");
scanf("%d",&n);
printf("the tower of hanoi involves the moves: \n \n");
start=clock();
towers(n,'A','c','B');
end=clock();
timetaken=(double)(end-start)/(CLOCKS_PER_SEC);
printf("the timetaken is %lf",timetaken);
return 0;
}
#include<stdlib.h>
#include<time.h>
void towers(int,char,char,char);
void towers(int n,char frompeg,char topeg,char auxpeg)
{
if(n==1)
{ printf("\n move disk 1 from peg %c to peg %c", frompeg,topeg);
return;
}
towers(n-1,frompeg,auxpeg,topeg);
printf("\n move disk %d from %c to peg %c",n,frompeg,topeg);
towers(n-1,auxpeg,topeg,frompeg);
}
int main()
{
int n;
time_t start,end;
double timetaken;
printf("enter the no of disks:");
scanf("%d",&n);
printf("the tower of hanoi involves the moves: \n \n");
start=clock();
towers(n,'A','c','B');
end=clock();
timetaken=(double)(end-start)/(CLOCKS_PER_SEC);
printf("the timetaken is %lf",timetaken);
return 0;
}
No comments:
Post a Comment