大一寒假集训2020.1.6 //栈

大一寒假集训2020.1.6

利用一组地址连续的存储单元依次自栈底到栈顶存放栈的数据元素.
在数组上实现时,栈底位置可以设置在数组的任一个端点,而栈顶是随着插入和删除而变化的,可以用一个整形变量top存放栈顶的指针,数据入栈或出栈时使整形变量 top分别加1或减1。

栈使用时遵循着,先进后出(后进先出)。

栈的基本操作:
(1)初始化栈 stackvis ,定义一个栈
(2)入栈 vis.push(x)
(3)出栈 vis.pop()
(4)判断是否为空 vis.empty()
(5)判断栈中元素的数量vis.size()
(6)得到栈的栈顶元素 vis.top()
综上: #include “stack”
用<bits/stdc++.h>则无需考虑头文件。

栈-程序员输入问题

简单的入栈出栈,将输入的入栈遇到提到的两个字符分别进行清空,退回就可。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<char> ch1;
    stack<char> ch2;
    char ch[110];
    gets(ch);
    int l = strlen(ch);
    for (int i = 0; i < l; i++)
    {
        if (ch[i] == '@')
        {
            while (!ch1.empty())
            {
                ch1.pop();
            }
            continue;
        }
        if (ch[i] == '#')
        {
            ch1.pop();
            continue;
        }
        ch1.push(ch[i]);
    }
    while (!ch1.empty())
    {
        ch2.push(ch1.top());
        ch1.pop();
    }
    int i = 0;
    while (!ch2.empty())
    {
        printf("%c", ch2.top());
        ch2.pop();
    }
    printf("\n");
    return 0;
}

栈-溶液模拟器

跟上一题一样的思路,只是多了判断条件,用P Z控制。

#include <bits/stdc++.h>
using namespace std;
double vtmp[10005];
double ctmp[10005];
int main()
{
    stack<double> v;
    stack<double> c;
    stack<double> tmp;
    double v0, c0;
    cin >> v0 >> c0;
    int n;
    cin >> n;
    for (int t = 0; t < n; t++)
    {
        char tmp;
        cin >> tmp;
        if (tmp == 'P')
        {
            double a, b;
            cin >> a >> b;
            v.push(a);
            c.push(b);
        }
        else if (tmp == 'Z')
        {
            if (!v.empty())
            {
                v.pop();
            }
            if (!c.empty())
            {
                c.pop();
            }
        }
        memset(vtmp, 0, sizeof(vtmp));
        memset(ctmp, 0, sizeof(ctmp));
        int tolv = 0;
        double tolc = 0;
        int s = v.size();
        for (int i = 0; i < s; i++)
        {
            vtmp[i] = v.top();
            v.pop();
            ctmp[i] = c.top();
            c.pop();
            tolv += vtmp[i];
            tolc += vtmp[i] * ctmp[i];
        }
        printf("%d %.5lf\n", tolv + (int)v0, (tolc + v0 * c0) / ((tolv + v0)));
        for (int i = s - 1; i >= 0; i--)
        {
            v.push(vtmp[i]);
            c.push(ctmp[i]);
        }
    }

    return 0;
}

栈-火车编组

不太容易想出来直接的处理办法。


//先逆序把1---n压入vis1栈;然后对vis2进行操作;  对于每个a[i], 把a[i]和vis2栈顶元素进行比较 ;
//看大小关系,小于a[i]就把vis1的元素往vis2里面放;大于的话,就把vis2的元素往vis1里面放!
// 相等就退出本次a[i]的循环!
#include <bits/stdc++.h>
using namespace std;
stack<int> vis1, vis2;
int a[200];
int main()
{
    int n;
    cin >> n;
    for (int i = n; i >= 1; i--)
        vis1.push(i);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        while (1)
        {
            if (vis2.empty())
            {
                vis2.push(vis1.top());
                vis1.pop();
                cout << 'A';
                continue;
            }
            if (vis2.top() < a[i])
            {
                vis2.push(vis1.top());
                vis1.pop();
                cout << 'A';
                continue;
            }
            if (vis2.top() > a[i])
            {
                vis1.push(vis2.top());
                vis2.pop();
                cout << 'B';
                continue;
            }
            if (vis2.top() == a[i])
            {
                vis2.pop();
                cout << 'B';
                break;
            }
        }
    }
    cout << endl;

    return 0;
}

栈-洗盘子

注意入栈顺序。

#include <bits/stdc++.h>
using namespace std;
stack<int> vis, vis1, vis2;
int main()
{
    int n, p, t;
    cin >> n;
    for (int i = n; i >= 1; i--)
        vis.push(i);

    while (cin >> p >> t)
    {

        if (p == 1)
        {
            for (int i = 1; i <= t; i++)
            {
                vis1.push(vis.top());
                vis.pop();
            }
        }
        if (p == 2)
        {
            for (int i = 1; i <= t; i++)
            {
                vis2.push(vis1.top());
                vis1.pop();
            }
        }
    }
    while (!vis2.empty())
    {
        cout << vis2.top() << endl;
        vis2.pop();
    }

    return 0;
}

栈-括号匹配

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[300];
int main()
{
    scanf("%s", s);
    int l = strlen(s);
    stack<char> q;
    char tmp;
    for (int i = 0; i < l; i++)
    {
        if (q.empty())
        {
            q.push(s[i]);
        }
        else
        {
            tmp = q.top();
            if ((tmp == '[' && s[i] == ']') || (tmp == '(' && s[i] == ')'))
            {
                q.pop();
            }
            else
            {
                q.push(s[i]);
            }
        }
    }
    if (q.empty())
    {
        printf("OK\n");
    }
    else
    {
        printf("Wrong\n");
    }
    return 0;
}

栈-表达式求值

这是不用栈写的

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e4;
char c;
int x, tmp, ans;
int main()
{
    scanf("%d", &x);
    tmp = x % mod;
    while (scanf("%c", &c) && c != '\n')
    {
        scanf("%d", &x);
        x = x % mod;
        if (c == '*')
            tmp = tmp * x % mod;
        else
        {
            ans = ans + tmp % mod;
            tmp = x % mod;
        }
    }
    ans = ans + tmp % mod;
    printf("%d\n", ans % mod);
    return 0;
}

写完这几个题其实还是不太明白栈的用法,还需要继续研究。
学习总要一点点来。

发布了7 篇原创文章 · 获赞 0 · 访问量 1316

猜你喜欢

转载自blog.csdn.net/qq_34212975/article/details/103942970