Winter vacation training in freshman year --- stack

Winter vacation training (7) - NEFU

order

Today's topic

1. Stack programmer input problem nefu 1624
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    stack < char >s1;
    stack < char >s2;
    char str[110];
    gets(str);
    int l=strlen(str);
    int i;
    for (i=0;i<l;i++)
    {
        if (str[i]=='@')
        {
            while (!s1.empty())
                s1.pop();//Need to cycle through the previous characters when @ is
                continue;
        }
        if (str[i]=='#')
        {
            if (!s1.empty())
                {
                    s1.pop();//Only need to cycle once when it is
                    continue;
                }
        }
        s1.push(str[i]);//The characters that are neither @ nor ා are stored in s1 as required characters, but pay attention to first in first out, so s1 cannot be output directly
    }
     while (!s1.empty())
     {
         s2.push(s1.top());
         s1.pop();//Because the stack is first in and last out, another stack is needed here to change the stored character order
     }
     while (!s2.empty())
     {
         printf("%c",s2.top());
         s2.pop();
     }
     printf("\n");
    return 0;
}

2. Stack bracket matching nefu 1630
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char str[256];
int main()
{
    scanf("%s",str);
    int l=strlen(str);
    stack<char>a;
    for(int i=0;i<l;i++)
    {
        if(a.empty())
        a.push(str[i]);
        else
        {
            if((a.top()=='['&&str[i]==']')||(a.top()=='('&&str[i]==')'))
            a.pop();
            else
            a.push(str[i]);
       }
    }
    if(a.empty())
    printf("OK\n");
    else
    printf("Wrong\n");
    return 0;
}
3. Stack expression evaluation nefu 1631

At first, I read the wrong question, thinking that there are four operations of addition, subtraction, multiplication and division. As a result, only multiplication and addition are included in the original question. 😦 ❤️ ❤️
There are five days left for the New Year! A kind of
☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,af,be,ans;
    char tmp;
    stack <int> s ;
    int ss;
    ss=0;
    ans=0;
    while (scanf("%lld",&a)!=EOF)
    {
        if (s.empty())
        {
            s.push(a);
            scanf("%c",&tmp);
        }
        else
        {
            if (tmp=='*')//Here I only wrote an = sign at the beginning, and then I found the bug for half an hour:)
            {
                af=s.top();
                s.pop();
                be=a;
                s.push((af%10000)*(be%10000));
            }
            else
            {
                s.push(a);
                //Scanf ("% C", & TMP); wrong position at the beginning:(
            }
            scanf("%c",&tmp);
        }
        if (tmp=='\n')
            break;//End the loop, otherwise the next while loop will not be possible
    }
    while (!s.empty())
    {
        ans+=s.top();
        s.pop();//The last while is multiplication, so the rest is all addition
    }
    printf("%lld\n",ans%10000);//Pay attention to the conditions in the question, only take the last four!!!
}

4. Stack solution simulator nefu 1627

❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️

#include <bits/stdc++.h>
using namespace std;
struct lay
{
    int v;
    double c;
};//Structure stack
stack<lay>vis;
int main()
{
    ios::sync_with_stdio(false);//Lifting speed
    char ch;
    int v1,v2,v0,v3;
    double c1,c2,c0,c3;
    int n;
    cin>>v0>>c0;
    v2=v0;
    c2=c0;
    cin>>n;
    while (n--)
    {
        cin>>ch;
        if (ch=='P')
        {
            cin>>v1>>c1;
            vis.push({v1,c1});
            c0=((c0*v0*0.01+c1*v1*0.01)/(v1+v0))*100;//Anti overflow
            v0+=v1;
            printf("%d %.5lf\n",v0,c0);
        }
        else
        {
            if (vis.empty())//Pay attention to the conditions in the questions and make judgment
            {
                printf("%d %.5lf\n",v2,c2);
                continue;
            }
            else
            {
                lay tmp=vis.top();
                vis.pop();
                v3=tmp.v;//structural morphology
                c3=tmp.c;
                c0=((v0*c0*0.01-v3*c3*0.01)/(v0-v3))*100.00;
                v0-=v3;
                printf("%d %.5lf\n",v0,c0);
            }
        }
    }
    return 0;
}

5. Stack wash dishes nefu 1629

Although this question uses three stacks, it is not as difficult as the first two questions

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);//Speed up
    stack <int> n0;
    stack <int> n1;
    stack <int> n2;//Using three stacks
    int n,i,j;
    int x,y;//Number of 1 wash and 2 wipe
    //int tmp1,tmp2;
    cin>>n;
    for(i=n; i>=1; i--)
        n0.push(i);//Order of unwashed dishes!!! Pay attention to the order of placing, one at the top
    //while (!n0.empty()||!n1.empty())
    while (cin>>x>>y)
    {
        if (x==1)
        {
            for (j=0; j<y; j++)
            {
                n1.push(n0.top());
                n0.pop();
            }
        }
        if (x==2)
        {
            for (j=0; j<y; j++)
            {
                n2.push(n1.top());
                n1.pop();
            }
        }
        if (n0.size()==0&&n1.size()==0)
            break;//Pay attention to end the cycle!!!
    }
    while (!n2.empty())
    {
        cout<<n2.top()<<endl;
        n2.pop();
    }
    return 0;
}

This is the end of the winter vacation training. In fact, the training has been completed on the 6th, and there is an exam on the afternoon of the 6th, so I haven't finished all the questions in the morning, and I just finished them recently. Please look forward to the next major ACM elective course

Published 6 original articles, won praise 3, visited 136
Private letter follow

Keywords: iOS REST simulator

Added by Chris16962 on Tue, 21 Jan 2020 18:50:10 +0200