Question

Shopping Spree: Acme Super Store is having a contest to give away shopping sprees to lucky...

Shopping Spree:

Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example, one family member can take one television, one watch and one toaster, while another family member can take one television, one camera and one pair of shoes. Each item has a price (in dollars) and a weight (in pounds) and each person in the family has a limit in the total weight they can carry. Two people cannot work together to carry an item. Your job is to help the families select items for each person to carry to maximize the total price of all items the family takes. Write an algorithm to determine the maximum total price of items for each family and the items that each family member should select.

a) A verbal description and give pseudo-code for your algorithm. Try to create an algorithm that is efficient in both time and storage requirements.


b) Implement your algorithm by writing a program named “shopping.cpp” (in C++) .

Input: The input file named “shopping.txt” consists of T test cases

* T (1 ≤ T ≤ 100) is given on the first line of the input file.


* Each test case begins with a line containing a single integer number N that indicates the number of items (1 ≤ N ≤ 100) in that test case


* Followed by N lines, each containing two integers: P and W. The first integer (1 ≤ P ≤ 5000) corresponds to the price of object and the second integer (1 ≤ W ≤ 100) corresponds to the weight of object.


* The next line contains one integer (1 ≤ F ≤ 30) which is the number of people in that family.


* The next F lines contains the maximum weight (1 ≤ M ≤ 200) that can be carried by the ith person in the family (1 ≤  i ≤ F).

Output: Written to a file named “results.txt”. For each test case your program should output the maximum total price of all goods that the family can carry out during their shopping spree and for each the family member, numbered 1 ≤ i ≤ F, list the item numbers 1 ≤ N ≤ 100 that they should select.

Sample Input
2
3
72 17
44 23
31 24
1
26
6
64 26
85 22
52 4
99 18
39 13
54 9
4
23
20
20
36


Sample Output:
Test Case 1
Total Price 72
Member Items
1: 1
Test Case 2
Total Price 568
Member Items

1: 3 4
2: 3 6
3: 3 6

4: 3 4 6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <string.h>
#include <iostream>
using namespace std;
void reverseString(string& str)
{ int n = str.length();
// Swap character starting from two
// corners
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
}
string* knapSack(int W, int wt[], int val[], int n,int count) /*return type is string because we have to return two value one is max value and second is index of weights*/
{
int i, w;
int K[n+1][W+1];
string s="";
static string a[2];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
{
K[i][w] = 0;
}
else if (wt[i-1] <= w)
{
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
}   
else
{
K[i][w] = K[i-1][w];
}
}
}

int res = K[n][W];
w = W;
for (i = n; i > 0 && res > 0; i--) {
/* either the result comes from the top (K[i-1][w]) or from (val[i-1] + K[i-1] [w-wt[i-1]]) as in Knapsack table. If it comes from the latter one/ it means the item is included. */
if (res == K[i - 1][w])
continue;   
else {
// This item is included.
s+=to_string(i);
s+=" ";
// Since this weight is included its
// value is deducted
res = res - val[i - 1];
w = w - wt[i - 1];
}
}

reverseString(s); //reverse the string

a[0]=to_string(count);//famly member index
a[0]+=" :";
a[0]+=s;
a[1]=to_string(K[n][W]);// max value
return a;
}
int main() {
int t;
cin>>t;
int c;
c=1;
while(t!=0)
{
int n;
cin>>n;
int val[100];
int weigth[100];
for(int i=0;i<n;i++)
{
cin>>val[i]>>weigth[i];
}
int familyperson;
cin>>familyperson;
int total;
total=0;
string a[30];
for(int i=0;i<familyperson;i++)
{
int W;
cin>>W;
string* p=knapSack(W,weigth,val,n,i+1);
a[i]=p[0];
total+=stoi(p[1]);
}
cout<<"Test Case "<<c<<endl;
c++;
cout<<"Total Price "<<total<<endl;
cout<<"Member Items"<<endl;
for(int z=0;z<familyperson;z++)
{
cout<<a[z]<<endl;
}
t--;
}
return 0;
}
/*

Input :-

2
3
72 17
44 23
31 24
1
26
6
64 26
85 22
52 4
99 18
39 13
54 9
4
23
20
20
36

Output:

Test Case 1
Total Price 72
Member Items
1 : 1
Test Case 2
Total Price 568
Member Items
1 : 3 4
2 : 3 6
3 : 3 6
4 : 3 4 6

*/

Add a comment
Know the answer?
Add Answer to:
Shopping Spree: Acme Super Store is having a contest to give away shopping sprees to lucky...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Problem, just need some guidance with the description, pseudocode and run time. I believe this...

    Python Problem, just need some guidance with the description, pseudocode and run time. I believe this is an 0-1 knapsack problem? Shopping Spree: (20 points) Acme Super Store is having a contest to give away shopping sprees to lucky families. If a family wins a shopping spree each person in the family can take any items in the store that he or she can carry out, however each person can only take one of each type of item. For example,...

  • This application is for you, the shopper in mind. Putting together an itemized shopping list that...

    This application is for you, the shopper in mind. Putting together an itemized shopping list that contains the items you need, the items you want, and the anticipated cost, this program will generate a total price. This way you can decide if your shopping budget fits the bill. Example list: Price List Binder paper: 2.29 Mop: 7.50 Scouring pads: 5 Your application program will create and write a new list to an output file in which the need and wish...

  • "Greedy, but Better": Given a knapsack problem with a weight capacity C, and n items, and...

    "Greedy, but Better": Given a knapsack problem with a weight capacity C, and n items, and each item has a weight W[1:n] and monetary value P[1:n]. You have to determine which items to take so that the total weight is C, and the total value (profit) is maximized. In this case we are considering an integer problem, so you can either take an item, or not take an item, you cannot take it fractionally. If you recall, the greedy algorithm...

  • There are n items in a store. For each item i=1,2,...,n the weight of the item...

    There are n items in a store. For each item i=1,2,...,n the weight of the item is wi and the value of the item is vi. A thief is carrying a knapsack of weight W. In this version of a problem the items can be broken into smaller pieces, so the thief may decide to carry only a fraction xi of object i, where 0≤xi ≤1. Item i contributes xiwi to the total weight in the knapsack, and xivi to...

  • Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights...

    Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights W1..n), all greater than 0 and one needs to maximize the total value of the subset of the items placed in the knapsack limited by a weight capacity of W In the 0-1 Knapsack Problem, each item must be either be included or excluded in its entirety, in light of the fact that this problem is to be "NP-Complete", how can one solve the...

  • Assume that an imaginary supermarket sells items that are identified by a unique item number which...

    Assume that an imaginary supermarket sells items that are identified by a unique item number which is an integer in range 100 to 499 inclusive. The items are classified in four different categories based on their numbers, and each category defines a different sales tax rate. These four categories along with the range of item numbers included in them, and their specific sales tax rates has been summarized in the following table:    Category Item Numbers Sales Tax Rate A                100...

  • Assume that you are developing a billing software for a departmental store which sells electronic appliances,...

    Assume that you are developing a billing software for a departmental store which sells electronic appliances, clothing, and toys. The store offers discounts on a festival occasion as below: a) 20 % discount on electronic appliances whose prices are greater than or equal to $100 b) 30 % discount on clothing if the price of a clothing item is greater than or equal to $50 c) All the toys greater than or equal to $20 are subject to 10% discount....

  • 8.7 LAB*: Program: Online shopping cart (Part 2)

    8.7 LAB*: Program: Online shopping cart (Part 2)Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized...

  • In the bin packing problem, items of different weights (or sizes) must be packed into a...

    In the bin packing problem, items of different weights (or sizes) must be packed into a finite number of bins each with the capacity C in a way that minimizes the number of bins used. The decision version of the bin packing problem (deciding if objects will fit into <= k bins) is NP-complete. There is no known polynomial time algorithm to solve the optimization version of the bin packing problem. In this practice problem you will be examining three...

  • ..Construct a solution algorithm for the following problem. Your solution should contain \: .Defi...

    ..Construct a solution algorithm for the following problem. Your solution should contain \: .Defining diagram .Pesudo code algorithm .Desk checking of the algorithm (three test case for each question including one "Invalid" Desk Checking) A transaction record on a sales commission file contains the retail price of an item sold, a transaction code which indicates the sales commission category to which an item can belong and the employee number of the person who sold the item. The transaction code can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT