I drink a lot of coke.I place the empty cans in a series of stacks, all in a straight line. When I view this series of stacks from the front, I can’t always see all the cans, or even all of the stacks, because sometimes a stack can be entirely obscured by a larger stack. When I view the series of stacks from the front, I can infer a minimum number of total cans. I can see each stack that is strictly larger than all the stacks between it and the front of the structure. For each stack I can see, I know how many cans are used to construct that stack. I then total up the number of cans that I know must exist: let’s call this number A. I then say I infer A cans in this structure.Suppose a series of stacks has the following stack sizes, in order from the front to the back: {1, 4, 3, 4, 6, 6, 2}. I’m able to infer 11 cans from this series: I can see the first, second, and fifth stacks (with 1, 4 and 6 cans respectively).Can you write a program that takes a series of stacks, and tells me how many cans I can infer?
#include <iostream>
#include<stack>
using namespace std;
int main(){
stack<int>stack1;
stack<int>stack2;
stack<int>stack3;
stack<int>stack4;
stack<int>stack5;
stack<int>stack6;
stack<int>stack7;
stack1.push(1);
cout<<"Length of stack 1: "<<stack1.size()<<endl;
for(int i=2; i<6; i++){
stack2.push(i);
}
cout<<"Length of stack 2: "<<stack2.size()<<endl;
for(int i=7; i<10; i++){
stack3.push(i);
}
cout<<"Length of stack 3: "<<stack3.size()<<endl;
for(int i=2; i<6; i++){
stack4.push(i);
}
cout<<"Length of stack 4: "<<stack4.size()<<endl;
for(int i=1; i<=6; i++){
stack5.push(i);
}
cout<<"Length of stack 5: "<<stack5.size()<<endl;
for(int i=1; i<=6; i++){
stack6.push(i);
}
cout<<"Length of stack 6: "<<stack6.size()<<endl;
for(int i=1; i<3; i++){
stack7.push(i);
}
cout<<"Length of stack 7: "<<stack7.size()<<endl;
int number= stack5.size() + stack2.size() + stack1.size();
cout<<"\n\nYou can infer "<<number<<" cans" <<endl;
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
C++