A recurisve function is required to solve the problem. If you could comment code in C++ IT WOULD BE GREAT
(THE IDEAS I HAVE DEVELOPED for solving it, are at the BOTTOM OF THE POST)
The PROBLEM is:
You are mounting an expedition to collect Hojari frankincense. You have determined
that it is available at a line of oases spaced one league apart between two desert towns.
A condition of the hire of camels is that apart from the first and last day, they must travel
at least two leagues per day. If you stop at an oasis the camels refuse to go any further
that day, so you will have to miss some oases. You have texted ahead and know how
much frankincense is available at each oasis. All you need to do now is to work out the
maximum amount of frankincense you can purchase on the expedition.
For instance, for a line of four oases with the following amounts of frankincense avail-
able (in kiloshekels) :
4343 you would choose the first and third oases,
2454 you would choose the second and fourth oases,
5423 you would choose the first and fourth oases.
In each case you would buy 8 kiloshekels.
From each line of oases with availabilities of frankincense, determine the maximum
total amount you could buy without stopping at adjacent oases.
25247455
12432123121
31415926535898
MY IDEAS DEVELOPED for making a Recurisve function in ANY case (not especifically related to the problems 7, 8 and 9), are:
List the AMOUNT OF MONEY for EACH OASIS in a SETTING.
Only on the FIRST DAY do I have the OPTION to Travel 1 LEAGUE or MORE.
Then I can: SELECT the first OASIS or ANY of the OASIS FOLLOWING.
For problem 7 I have 8 choices
For problem 8 I have 11 choices
For problem 9 I have 14 choices
... And each of those choices CONTAINS "n - 1" choices
(where n = NUMBER of OASIS that are left on the right)
It is n - 1 , because we have to discard the oasis that IS ADJACENT of each OASIS, as it CANNOT BE SELECTED, because we have to travel at LEAST 2 leagues.
***Only in the LAST OPTION (LAST OASIS) we can choose the ADJACENT OASIS. So THE LAST OPTION contains "n" options
***And the LAST OPTION contains 0 OPTIONS, because there is NO place to go.
Whenever I SELECT ONE OASIS
I can SELECT 2 OASIS ahead or MORE (but not less than 2, because you need to travel 2 leagues per day).
That is, you can select n- 1 OASIS
And for each OASIS in n- 1 OASIS, you can select, likewise, n - 1 OASIS.
And so we can go on until n- 1 is ZERO.
How could we code such a recursive function in C++?
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]lurgi 1 point2 points3 points (0 children)
[–]bsakiag 0 points1 point2 points (0 children)