English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

You’re in charge of buying coffee beans for your local coffee shop. In front of you are N piles of coffee beans. Each pile has a certain weight (for the whole pile), and an amount of profit you can make selling the coffee in that pile to people in your coffee shop. So, for example, you could have 3 piles:
a. Pile 1: Weighs 2 pounds, Sells for $5
b. Pile 2: Weighs 3 pounds, Sells for $4
c. Pile 3: Weighs 4 pounds, Sells for $7

You’re shopping yourself so you can only carry a certain amount of beans. (Suppose that amount is stored in a variable called “max_weight”. What should you take?)
(In the example above, if “max_weight” = 4 pounds, I should take all of pile 1, and half of pile 3, for a total profit of $8.50)

2007-11-11 21:16:37 · 4 answers · asked by Jack Phan 1 in Computers & Internet Programming & Design

4 answers

Easy one mate :)

// The user enters the amount of piles:
int aop = Integer.parseInt(inputbox1.getvalue);

// The user enters the max weight:
double max_weight = Double.parseDouble(inputbox2.getvalue); // change this to something that resembles an input


// Initialise Variables
double[] weights = new double[aop];
double[] money = new double[aop];
// Somehow you will have to input the weights and money e.g.
weights[0] = Double.parseDouble(inputboxw[0].value);
.
.
.
.
money[0] = Double.parseDouble(inputbox[m].value);
.
.
.

boolean carryon = true;
char[] full_half = new char[aop];

int[] temppile = new int[0];
int[] temppile2 = new int[0];
double weightleft = max_weight;
double currentmoney = 0;
int best_in_loop = 0;

while (carryon == true) {
carryon = false; // if we don't find anything exit
// First add all the whole piles
for (int i = 0; i < aop; i++) {
if (money[i] > currentmoney && weights[i] < max_weight && full_half[i] != 'F') {
currentmoney = money[i];
best_in_loop = i;
carryon = true; // found one that can fit!
} //end if
} // end for
full_half[best_in_loop] = 'F'; // F for FULL
max_weight -= weights[best_in_loop];

} // end while
// By now we should have a list of F and blanks (''), e.g. for your example we would have full_half[0] being 'F' while the other two are ''.

//Now to test for any remainders
carryon = true;
currentmoney = 0;

while (carryon == true) {
carryon = false; // if we don't find anything exit
// Add all the half piles
for (int i = 0; i < aop; i++) {
if (money[i]/2 > currentmoney && weights[i]/2 < max_weight && full_half[i] != 'F' && full_half[i] != 'H') {
currentmoney = money[i]/2;
best_in_loop = i;
carryon = true; // found one that can fit!
} //end if
} // end for
full_half[best_in_loop] = 'H'; // F for FULL
max_weight -= weights[best_in_loop]/2;

} // end while

/*
So now, we should have full_half filled with best F and H which stands for Full and Half.
*/

// Therefore:
String output = "The best possible combination is:"
double totmoney = 0;
for (int z = 0; z < aop; z++) {
if (full_half[z] == 'F') {
output += " Take Pile "+z;
totmoney += money[z];
} // end if
else if (full_half[z] == 'H') {
output += " Take half of Pile "+z;
totmoney += money[z]/2;
} // end else if
} // end for loop
output += " for a total of: $"+ totmoney;

// Thats it mate, there may be a few mistakes in there, but since it is pseudocode it should be fine, the logic is all there

2007-11-12 01:24:17 · answer #1 · answered by Markos K 3 · 0 0

Jack my friend I don't have time to do that but here is your link. LEARN

http://www.google.co.uk/search?hl=en&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=how+to+write+pseudocode&spell=1

2007-11-12 05:21:15 · answer #2 · answered by Joe_Young 6 · 0 1

We've already got our degrees and we earned them the hard way. Hope you will too!

2007-11-12 05:45:03 · answer #3 · answered by John K 6 · 0 0

Greedy Algorithm, that is it.

2007-11-12 05:29:08 · answer #4 · answered by Andy T 7 · 1 0

fedest.com, questions and answers