Write a C++ program that asks the user to enter an integer n followed by n numbers. The program will use 2 stacks to enter the numbers entered by the user. In every iteration the program will balance the total of the numbers stored in every stack. For example, if the user already entered 10 (the program stores this value in stack 1) and 23.5 (the program stores this value in stack 2) and then enters the value 4.7 then the program will store this value in stack 1 since 10<23.5. If the user enters 12.3, then the program will store it in stack 1 since the total in stack 1 is 14.7 (10+4.7) and 14.7<23.5. If the user enter 7, then the program will store this value in stack 2 since the total in stack 1 is 27 (14.7+12.3) and 27>23.5.
At the end, the program will display on the screen the content of every stack.

i try this

    #include<iostream>  
    #include<stack>
    using namespace std; 
    int main()
    {    


          int n ,m1,m2;      
        cout<<"Enter an integer:";  
        cin>>n;
         stack<double> q;
          stack<double>w;
          for( int i=0;i<n;i++)
          {          cin>>m1;
               q.push(m1);
               }
      for( int i=0;i<n;i++)
      {
           q.push(m1);
           q.pop();
         }
          for( int j=0;j<n;j++)
          {
              cin>>m2;
              w.push(m2);
              }
          for( int j=0;j<n;j++)
      {
           w.push(m2);
           w.pop();
         }    
    double k=q.front();
    while( !q.empty())
    {if(q.front()>m2)

           q.top( );
           q.pop();
           cout<<"The content of stack 1 is :"<<q.top()<endl;
           }

    cout<<endl;


    cout<<"The content of stack 2 is :"<<w<<endl;
      system("pause");
         return 0;
    }

Edited 4 Years Ago by deceptikon: Fixed code tags

I'd use another set of variables to keep track of the total value of the ints in both stacks and use those result to determine where to push the next input.

This article has been dead for over six months. Start a new discussion instead.