2020.1.6 大一寒假训练(栈)

填坑。大一寒假培训第一部分Day7——栈,未开Contest。

Problem A:NEFU1624 程序员输入问题

数组:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    char ch, str[110];
    int tmp=0;
    while(cin>>ch)
    {
        if(ch == '@') tmp=0;
        else if(ch == '#') tmp--;
        else str[tmp++] = ch;
    }
    for(int i=0; i<tmp; i++)
        cout<<str[i];
    return 0;
}

STL:(略长)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    stack<char> vis1; 
    stack<char> vis2;
    string str;
    //cin>>str;
    getline(cin, str);
    for(int i=0; i<str.size(); i++)
    {
        if(str[i] == '@')
        {
            while(!vis1.empty())
                vis1.pop();
            continue;
        }
        if(str[i] == '#')
            if(!vis1.empty())
            {
                vis1.pop();
                continue;
            }
        vis1.push(str[i]);
    }
    while(!vis1.empty())
    {
        vis2.push(vis1.top());
        vis1.pop();
    }
    while(!vis2.empty())
    {
        cout<<vis2.top();
        vis2.pop();
    }
    return 0;
}

Problem B:NEFU1627 溶液模拟器

#include<bits/stdc++.h>
using namespace std;
struct node{
    int v;
    double c;
};
int n, v0, v1, vv, v2;
char p;
double c0, c1, cc, c2;
stack<node> s;
int main()
{
    ios::sync_with_stdio(false);
    cin>>v0>>c0;
    vv = v0;
    cc = c0;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>p;
        if(p == 'P')
        {
            cin>>v1>>c1;
            s.push({v1, c1});
            c0 = (v0*c0*0.01+v1*c1*0.01)/(v0+v1);
            c0 *= 100;
            v0 += v1;
            printf("%d %.5lf\n", v0, c0);
        }
        else
        {
            if(s.empty()) 
            //当只剩初始溶液的时候,再撤销就没有用了,这时只输出初始的体积和浓度。
            {
                printf("%d %.5lf\n", vv, cc);
                continue;
            }
            node tmp = s.top();
            s.pop();
            v2 = tmp.v;
            c2 = tmp.c;
            c0 = (v0*c0*0.01-v2*c2*0.01)/(v0-v2);
            c0 *= 100;
            v0 -= v2;
            printf("%d %.5lf\n", v0, c0);
        }
    }
    return 0;
}

Problem C:NEFU1628 火车编组

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
stack<int> v1, v2;
int n, num[105];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=n; i>=1; i--)
        v1.push(i);
    for(int i=1; i<=n; i++)
        cin>>num[i];
    for(int i=1; i<=n; i++)
        while(1)
        {
            if(v2.empty())
            {
                v2.push(v1.top());
                v1.pop();
                cout<<"A";
                continue;
            }
            if(v2.top() < num[i])
            {
                v2.push(v1.top());
                v1.pop();
                cout<<"A";
                continue;
            }
            if(v2.top() > num[i])
            {
                v1.push(v2.top());
                v2.pop();
                cout<<"B";
                continue;
            }
            if(v2.top() == num[i])
            {
                v2.pop();
                cout<<"B";
                break;
            }
        }
    cout<<endl;
    return 0;
}

Problem D:NEFU1629 洗盘子

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
stack<int> plate, washed, clean;
int main()
{
    ios::sync_with_stdio(false);
    int n, flag, num;
    cin>>n;
    for(int i=n; i>=1; i--)
        plate.push(i);
    while(1)
    {
        cin>>flag>>num;
        if(flag == 1)
        {
            while(num--)
            {
                washed.push(plate.top());
                plate.pop();
            }
        }
        if(flag == 2)
            while(num--)
            {
                clean.push(washed.top());
                washed.pop();
            }
        if(clean.size()==n) break;
    }
    while(!clean.empty())
    {
        cout<<clean.top()<<endl;
        clean.pop();
    }
    return 0;
}

Problem E:NEFU1630 括号匹配

数组:

#include<bits/stdc++.h>
using namespace std;
char a[260], str[260];
int main()
{
    ios::sync_with_stdio(false);
    int i=0, top=0;
    bool flag=1;
    scanf("%s", a);
    while(i<strlen(a) && flag)
    {
        if(a[i]=='[' || a[i]=='(')
            str[++top] = a[i];
        if(a[i]==']')
        {
            if(str[top]=='[') top--;
            else flag=0;
        }
        if(a[i]==')')
        {
            if(str[top]=='(') top--;
            else flag=0;
        }
        i++;
    }
    if(flag && !top) cout<<"OK";
    else cout<<"Wrong";
    return 0;
}

STL:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    stack<char> s1;
    char str[260];
    scanf("%s", str);
    bool flag=1;
    int i=0, n=strlen(str);
    while (i<n && flag)
    {
        if(str[i]=='[' || str[i]=='(')
            s1.push(str[i]);
        //if(!s1.empty())
        //{
            if(str[i]==']')
            {
                if(!s1.empty() && s1.top()=='[') s1.pop();
                else flag=0;
            }
            if(str[i]==')')
            {
                if(!s1.empty() && s1.top()=='(') s1.pop();
                else flag=0;
            }
        //}
        i++;
    }
    if(flag && s1.empty()) cout<<"OK";
    else cout<<"Wrong";
    return 0;
}

Problem F:NEFU1631 表达式求值

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
//typedef long long ll;
const int mod = 1e4;
stack<int> num;
//stack<char> sym;
int main()
{
    ios::sync_with_stdio(false);
    char ch;
    int n;
    cin>>n;
    num.push(n);
    while(cin>>ch)
    {
        if(ch == '\n') break;
        cin>>n;
        if(ch == '+')
            num.push(n);
        else
        {
            int t = num.top();
            num.pop();
            num.push(n*t%mod);
        }
    }
    int ans = 0;
    while(!num.empty())
    {
        ans += num.top();
        num.pop();
    }
    cout<<ans%mod<<endl;
    return 0;
}
发布了65 篇原创文章 · 获赞 50 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/baidu_41248654/article/details/104663035