forming a recursion function C++ programming -
a student can things bellow:
a. homework in 2 days
b. write poem in 2 days
c. go on trip 2 days
d. study exams 1 day
e. play pc games 1 day
a schedule of n days can completed combination of activities above. example 3 possible schedules 7 days are:
homework, poem, homework, play poem, study, play, homework, study trip, trip, trip, study find recursive function t(n) represents number of possible schedules n days.
i have 2 questions...
firstly had write c++ program this..
#include<iostream> using namespace std; int count(int n) { if (n < 1) return 0; if (n == 2 || n == 1) return 1; return (3*(count(n-2))) + (2*(count(n-1))); } int main() { cout<<count(2); }
for input 2 it's giving answer 1.. shouldn't 7? homework ; poem ; trip ; exams,pcgame ; pcgame,exams; pcgame,pcgame; exams,exams
secondly, suppose consider pcgame, trip , trip,pcgame
same combinations. how formulate recursive solution that?
when n = 2
following if
condition find true
paths,
if (n == 2 || n == 1) return 1; ^^^^^
hence returns 1
, terminates recursion.
Comments
Post a Comment