The problem statement for staircase problem is
Suppose there are n stairs and a person wants to reach the top. The person can climb either 1 stair or 2 stairs or 3 stairs at a time. Find the minimum number of steps to reach the top.
#include<stdio.h>
int min(int a,int b,int c)
{
if(a<=b && a<=c)
return a;
else if(b<=c && b<=a)
return b;
else if(c<=a && c<=b)
return c;
}
int minm_stairs(int total)
{
int steps;
if(total==0)
return 0;
else if(total<0)
return 32767;
else
{
steps=min(minm_stairs(total-1)+1,
minm_stairs(total-2)+1,minm_stairs(total-3)+1);
return steps;
}
}
void main()
{
int n,minm;
printf("Enter total number of stairs\n");
scanf("%d",&n);
minm=minm_stairs(n);
if(minm!=32767)
printf("Minimum steps required to reach at top are %d",minm);
else
printf("No way found\n");
}
No comments:
Post a Comment