Count number of ways to cover a distance | A naive recursive C ++ program to count number of ways to cover a distance with 1 , 2 and 3 steps ; Returns count of ways to cover ' dist ' ; Base cases ; Recur for all previous 3 and add the results ; driver program
#include <iostream> NEW_LINE using namespace std ; int printCountRec ( int dist ) { if ( dist < 0 ) return 0 ; if ( dist == 0 ) return 1 ; return printCountRec ( dist - 1 ) + printCountRec ( dist - 2 ) + printCountRec ( dist - 3 ) ; } int main ( ) { int dist = 4 ; cout << printCountRec ( dist ) ; return 0 ; }