Created
September 17, 2017 03:29
-
-
Save RohithKumar1368/b44af5579d5ac17913cd1b7fb8735564 to your computer and use it in GitHub Desktop.
program to find the number of ways we can climb n number of steps, given that we can take k maximum steps at a time. Also we cannot take a 0 number step.Solution using dynamic programming.
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
#include<iostream> | |
#include<algorithm> | |
#include<string> | |
#include<vector> | |
#include<cmath> | |
#include<time.h> | |
using namespace std ; | |
int pow(int n, int k){ | |
int return_value = 1 ; | |
if(k == 0) return 1 ; | |
for(int i = 0 ; i < k ; i++){ | |
return_value *= n ; | |
} | |
return return_value ; | |
} | |
int main() { | |
unsigned long int number ; | |
cout << "Enter the number of steps to climb" << endl ; | |
cin >> number ; | |
unsigned long int k ; | |
cout << "Enter the maximum number of steps possible at a time" << endl ; | |
cin >> k ; | |
clock_t t_start = clock() ; | |
unsigned long int *s = new unsigned long int[number+1] ; | |
for(long int i = 1 ; i <= k ; i++){ | |
s[i] = pow(2,i-1) ; | |
} | |
for(long int i = k+1 ; i <= number ; ++i){ | |
for(long int j = i-1 ; j >= i-k ; j--){ | |
s[i] += s[j] ; | |
} | |
} | |
cout << "The number of ways are " << s[number] << endl ; | |
cout << double(clock() - t_start) / (double)CLOCKS_PER_SEC << " seconds." << endl ; | |
return 0 ; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment