0% found this document useful (0 votes)
86 views3 pages

PDF Project

This program collects the maximum number of apples that can be picked from boxes given certain rules. It takes in the number of boxes and count of apples in each box from user input. It generates all possible permutations of picking from boxes, filters out invalid picks based on rules, and finds the permutation with the highest sum, outputting the boxes picked and total apples.

Uploaded by

api-498226155
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views3 pages

PDF Project

This program collects the maximum number of apples that can be picked from boxes given certain rules. It takes in the number of boxes and count of apples in each box from user input. It generates all possible permutations of picking from boxes, filters out invalid picks based on rules, and finds the permutation with the highest sum, outputting the boxes picked and total apples.

Uploaded by

api-498226155
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

* Title: hw2_2.cpp
* Abstract: A program that collects the maximum number of apples in boxes.
* Author: Ericka Koyama
* ID: 0728
* Date: 03/16/2021
1 */
2
3 #include <iostream>
4 #include <math.h> // pow
5 #include <string>
6 #include <vector>
7
8 using namespace std;
9
10 void generatePermutations(vector<vector<int>> &permutations, int numElements) {
11 int numPermutations = pow(2, numElements) - 1;
12 int divResult;
13 int k;
14 vector<int> curBinaryDigits;
15
16 for (int i=0; i <= numPermutations; i++) {
17 curBinaryDigits.assign(numElements, 0);
18
19 /**
20 * Convert each i to binary representation
21 */
22 divResult = i / 2;
23 int k = numElements - 1;
24 curBinaryDigits.at(k) = i % 2 == 1 ? 1 : 0;
25
26 while (divResult != 0) {
27 k--;
28 curBinaryDigits.at(k) = divResult % 2 == 1 ? 1 : 0;
29 divResult = divResult / 2;
30 }
31
32 permutations.push_back(curBinaryDigits);
33 curBinaryDigits.clear();
34 }
35 }
36
37 int getPermutationSum(vector<int> &v, vector<int> &permutation) {
38 int sum = 0;
39
40 for (int i=0; i < permutation.size(); i++) {
41 // if index in permutation is 1, we include that index from v in sum
42 if (permutation.at(i) == 1) {
43 sum += v.at(i);
44 }
45 }
46
47 return sum;
48 }
49
50 int main()
51 {
52 int numBoxes; // max 15 boxes
53 int curBoxCount; // the count of items in a single box
54 vector<int> boxes;
55
56 int numPermutations; // num of possible combinations of picks
57 vector<vector<int>> boxPermutations; // 2D vector like [[1, 0, 1], [1, 0, 0]]
58 vector<vector<int>> filteredPermutations; // 2D vector like [[1, 0, 1], [1, 0, 0]], filtered to picking rules
59
60 // Take user input, 1st line is numBoxes
61 cin >> numBoxes;
62
63 // Next lines are each boxes content count
64 for (int i=0; i < numBoxes; i++) { // fill boxes
65 cin >> curBoxCount;
66 boxes.push_back(curBoxCount);
67 }
68
69 // generate permutations
70 generatePermutations(boxPermutations, numBoxes);
71
72 // Filter permutations to only ones that meet picking rules
73 for (int i=0; i < boxPermutations.size(); i++) {
74 vector<int> curBoxPerm = boxPermutations.at(i);
75 bool isValidPick = true;
76 // every time encounter a 1, check if there's a 1 next to it, if so, disqualify
77 for (int j=0; j < curBoxPerm.size(); j++) {
78 if (curBoxPerm.at(j) == 1 && j < curBoxPerm.size() - 1) { // ignore check at last box
79 // check the next box
80 if (curBoxPerm.at(j + 1) == 1) {
81 isValidPick = false;
82 break; // break loop since we know this permutation is not valid
83 }
84 }
85 }
86
87 if (isValidPick) {
88 filteredPermutations.push_back(curBoxPerm);
89 }
90 }
91
92 // Find max by summing 1's
93 int curMax = getPermutationSum(boxes, filteredPermutations.at(1)); // Index=1 max by default, skip all 0 version
94 int winningPermutationIndex = 1; // index of current wining permutation (most apples), skip all 0 version
95
96 for (int i=2; i < filteredPermutations.size(); i++) {
97 int sum = getPermutationSum(boxes, filteredPermutations.at(i));
98
99 if (sum > curMax) {
100 curMax = sum;
101 winningPermutationIndex = i;
102 }
103 }
104
105 // Print result
106 cout << "Boxes:";
107 // Print 1's in winning permutation
108 vector<int> winningPermutation = filteredPermutations.at(winningPermutationIndex);
109 for (int i=0;i < winningPermutation.size(); i++) {
110 if (winningPermutation.at(i) == 1) {
111
112 cout << i << " ";
113 }
114 }
115 cout << endl;
116
117 cout << "Max Apples:" << curMax << endl;
118 return 0;
119 }
120
121

PDF document made with CodePrint using Prism

You might also like