'I am trying to print out user input to find the max amount of apples I can get in the boxes but can't get the boxes to print correctly
My code at the moment prints out the max amount of apples but not the correct boxes.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
int i = 0;
cin >> n;
int apples[n];
for (int i = 0; i < n; i++)
cin >> apples[i];
int max_including = apples[0];
int max_excluding = 0;
for (int i = 1; i < n; i++)
{
int temp = max(max_including, max_excluding);
max_including = max_excluding + apples[i];
max_excluding = temp;
}
cout << "Boxes:";
cout << apples[0] << " ";
for (i = 0; n < i; i++)
{
if (apples[i] > 0)
{
cout << apples[i] << " ";
}
}
int ans = max(max_including, max_excluding);
cout << "\nMax Apples:" << ans;
return 0;
}
I have one constraint to where if a box is used, I must skip at least the next box. Some samples of what I should get are: Input
4
3
2
3
5
The output should return
Boxes:0 3
Max Apples:8
However I can only get mine to return
Boxes:3
Max Apples:8
Another sample is: Input
1
500
The output should return
Boxes:0
Max Apples:500
However I can only get mine to return
Boxes:500
Max Apples:500
How would I fix this code to properly output the correct corresponding boxes? Thank you in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
