1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <array>
#include <cstring>
#include <cassert>
void test();
std::array<int, const int numberOfWeights> getContainerInput(const int numberOfWeights);
int getMaxNumOfFruits(const int bellySize, const std::array<int, const int size> weights);
int main(int argc, char* argv[]) {
if (argc > 1 && strncmp(argv[1], "test", 4) == 0) {
test();
}
else {
int numberOfWeights = 0, sizeOfBelly = 0;
std::cin >> numberOfWeights >> sizeOfBelly;
const int size = numberOfWeights;
std::array<int, size> weights = getContainerInput(numberOfWeights);
std::cout << getMaxNumOfFruits(sizeOfBelly, weights) << '\n';
}
}
std::array<int, const int numberOfWeights> getContainerInput(const int numberOfWeights) {
std::array<int, numberOfWeights> fruitWeights;
for (int i = 0; i < numberOfWeights; ++i) {
std::cin >> fruitWeights[i];
}
return fruitWeights;
}
int getMaxNumOfFruits(const int bellySize, const std::array<int, const int size> weights) {
const int numOfWeights = weights.size();
int maxNumOfFruitsEaten = 0;
bool continueLoop = true;
for (int i = 0; i < numOfWeights && continueLoop == true; ++i) {
int weightOfFruitsEaten = 0, countOfFruitsEaten = 0;
for (int weightPosition = i; weightPosition < numOfWeights && continueLoop == true; ++weightPosition) {
if (weightOfFruitsEaten + weights[weightPosition] <= bellySize) {
weightOfFruitsEaten += weights[weightPosition];
++countOfFruitsEaten;
if (weightOfFruitsEaten == bellySize) {
continueLoop = false;
}
}
}
if (countOfFruitsEaten > maxNumOfFruitsEaten) {
maxNumOfFruitsEaten = countOfFruitsEaten;
}
}
return maxNumOfFruitsEaten;
}
void test() {
int numOfWeights = 10, bellySize = 3;
//const int size = numOfWeights;
std::array<int, numOfWeights> weights = {1, 2, 3, 4, 5, 6, 7, 8, 9};
assert(getMaxNumOfFruits(bellySize, weights) == 2);
bellySize = 10;
assert(getMaxNumOfFruits(bellySize, weights) == 4);
bellySize = 60;
assert(getMaxNumOfFruits(bellySize, weights) == 10);
}
|