Question
Here we are given a series of menu items and a target amount W (cents). We want to know if it is possible to spend exactly W cents, and, if so, what is the minimum number of calories with which it is possible to do so. Each input will represent a single menu and have a single target value. The first line of the input contains a number N, the number of menu items. The second line contains a target amount W. Each of the following N lines contain two integers and a string: V C S. V is the value (or cost) of the item, in cents; C is the number of calories for that item; and S (a string) is the name of the item.
Input sample:
6
1900
100 200 burrito
200 350 taco
400 500 chimi
150 300 horchata
500 650 torta
300 400 cola
Your output should provide the minimum number of calories on which it is possible to spend exactly W cents. If it is not possible to spend exactly W cents, then indicate “not possible” in your output. When it is possible to spend that amount, you will need to list the menu items chosen that add up to W cents. You should provide both an iterative implementation and a memoized one. They will probably give the same output (but could be different).
RECURRENCE
Here is a basic form of a subproblem and recurrence: For a subproblem, we define MCAL[w] be the minimum number of calories on which it is possible to spend exactly w cents. For the recurrence, let’s first denote the input by vectors v and c, where v[i] is the value (or cost) of menu item i, and c[i] is the calorie content of that menu item. Now we can write the recurrence as ⁃ if w<0, MCAL[w] = infinity ⁃ if w=0, MCAL[w] = 0 ⁃ else, MCAL[w] = MIN { MCAL[w-v[i]] + c[i] : 1<= i <= n } You may want to alter the recurrence to make it easier to manipulate, but this is a simple form of it.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
#include <fstream>#include <iostream>
using namespace std;
const int INFINITY = 1000000000;
// struct to describe menu item
struct MenuItem
{
int cost;
int cals;
char name[30];
};
// determines which menu items are in the solution menu
void GetElements(MenuItem items[], int j, int n, int * usedItems)
{
int * elements = new int[n];
for (int i = 0; i < n; i++)
{
elements[i] = 0;
}
while (j >= 0)
{
int tmp = usedItems[j];
if (tmp == -1) break; // if the item is not on menu
elements[tmp] += 1;
j -= items[usedItems[j]].cost;
}
for (int i = 0; i < n; i++) //Print the result
{
if (elements[i] > 0)
{
cout<< items[i].name << " " << elements[i] << endl;
}
}
cout << endl;
delete[] elements;
}...