Nuts in an Oasis

A pile of nuts is in an oasis, across a desert from a town. The pile contains ‘N’ kg of nuts, and the town is ‘D’ kilometers away from the pile.

The goal of this problem is to write a program that will compute ‘X’, the maximum amount of nuts that can be transported to the town.

The nuts are transported by a horse drawn cart that is initially next to the pile of nuts. The cart can carry at most ‘C’ kilograms of nuts at any one time. The horse uses the nuts that it is carrying as fuel. It consumes ‘F’ kilograms of nuts per kilometer traveled regardless of how much weight it is carrying in the cart. The horse can load and unload the cart without using up any nuts.

Your program should have a function that takes as input 4 real numbers D,N,F,C and returns one real number: ‘X’:

IDEA:1. X is a monotonic increasing function of N. A little increasing of N could increase X.

2. The distance to travel is a problem. It should be the minimum of the distance to the destination and the distance consume of enough nuts such that the next round we could travel 1 less round.

01 double maxNut(double N, double D, double C, double F)
02 {
03     double t=C;
05     if(N<=C){
06         if(F*D>=N)
07             return 0.0;
08         else
09             return NF*D;
10     }
11     int k=ceil(N/C);
13     double consumperkilometer=F*(2*k1);
14     double remain=k*CC;
15     double distancetotravel=(Nremain)/consumperkilometer;
17     if(distancetotravel<D)
18         return maxNut(remain,Ddistancetotravel,C,F);
19     else
20         return NF*(2*k1)*D;
21 }
This entry was posted in Recursive. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s