2. The distance from Point A to Point B is n meters(n>2). A person goes from point A to point B walking by taking steps of size 2 meters or 3 meters. Please write a program to find the total number of ways in which the person can do this. The program should take n as input. For e.g., if input n = 5 the output should be 2, since the person can do this in two ways:
1. Take 2 steps followed by 3 steps.
2. Take 3 steps followed by 2 steps.
#include <stdio.h>
int main() {
int n;
printf("%d", &n);
int steps[n];
steps[0] = steps[1] = 0;
steps[2] = 1;
for (int i = 3; i < n; i++) {
steps[i] = stets[i-2] + steps[i-3] + 2;
}
printf("%d\n", steps[n-1]);
return 0;
}
Comments
Leave a comment