Skip to main content

All you can eat....new algorithm

The idea is to eat at least one of each type of food item without overspending where N is the limit.Also it is required to get the maximum satisfaction at the same time, as indicated by the respective items desirability score and find that score.

Appetizer    Desirability score

A ($7)    10
B ($9)    11
C ($10)    13
D ($6)    9
E ($4)    3

Main Course:

R ($20)    13
S ($25)    23
T ($21)    14
U ($15)    8
V ($18)    12

Desert:

I ($3)    5
J ($4)    6
K ($5)    8
L ($7)    9
M ($8)    11


Here is a generic function that does the work in C#.

public void GetMaxDesirability(int N)
{

ArrayList ar = new ArrayList();
double sm = 0;

Hashtable ht1 = new Hashtable();
Hashtable ht2 = new Hashtable();
Hashtable ht3 = new Hashtable();

int[] appetizer = { 7, 9, 10, 6, 4 };

ht1["7"] = 10;
ht1["9"] = 11;
ht1["10"] = 13;
ht1["6"] = 9;
ht1["4"] = 3;

int[] maincourse = { 20, 25, 21, 15, 18 };

ht2["20"] = 13;
ht2["25"] = 23;
ht2["21"] = 14;
ht2["15"] = 8;
ht2["18"] = 12;

int[] desert = { 3, 4, 5, 7, 8 };

ht3["3"] = 5;
ht3["4"] = 6;
ht3["5"] = 8;
ht3["7"] = 9;
ht3["8"] = 11;

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{

sm = appetizer[i] + maincourse[j] + desert[k];

if (sm > N)
{
continue;
}
else
{
int aa = int.Parse(ht1[appetizer[i].ToString()].ToString()) + int.Parse(ht2[maincourse [j].ToString()].ToString()) + int.Parse(ht3[desert[k].ToString()].ToString());

ar.Add(aa);

}
}
}
}

MessageBox.Show(FindMax(ar).ToString());

}

Comments