大一寒假集训(7)---栈

大一寒假集训(7)—栈 NEFU

在这里插入图片描述

今日题目

在这里插入图片描述

1.栈-程序员输入问题 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();//为@时需多次循环清空之前的字符
                continue;
        }
        if (str[i]=='#')
        {
            if (!s1.empty())
                {
                    s1.pop();//为#时只需循环一次即可
                    continue;
                }
        }
        s1.push(str[i]);//既不为@也不为#的字符存入s1中为需要的字符,但注意先进后出,所以不能直接输出s1
    }
     while (!s1.empty())
     {
         s2.push(s1.top());
         s1.pop();//因为栈为先进后出所以这里需要另外一个栈来是存入的字符换顺序
     }
     while (!s2.empty())
     {
         printf("%c",s2.top());
         s2.pop();
     }
     printf("\n");
    return 0;
}

2.栈-括号匹配 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.栈-表达式求值 nefu 1631

我一开始读错题目,以为有加减乘除四种运算,结果原题中只有乘法和加法。😦 ❤️ ❤️
还有5天就过年了!😃
☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️☀️

#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=='*')//此处我一开始只写了一个=号,然后我找了半个小时的bug:)
            {
                af=s.top();
                s.pop();
                be=a;
                s.push((af%10000)*(be%10000));
            }
            else
            {
                s.push(a);
                //scanf("%c",&tmp);一开始加错位置:(
            }
            scanf("%c",&tmp);
        }
        if (tmp=='\n')
            break;//结束循环,不然就一直无法进行下一个while 循环
    }
    while (!s.empty())
    {
        ans+=s.top();
        s.pop();//上一个while进行的是乘法运算,所以剩下的全是加法运算
    }
    printf("%lld\n",ans%10000);//注意题中条件,只取后四位!!!
}

4.栈-溶液模拟器 nefu 1627

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

#include <bits/stdc++.h>
using namespace std;
struct lay
{
    int v;
    double c;
};//结构体入栈
stack<lay>vis;
int main()
{
    ios::sync_with_stdio(false);//提升速度
    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;//防溢出
            v0+=v1;
            printf("%d %.5lf\n",v0,c0);
        }
        else
        {
            if (vis.empty())//注意题中条件并进行判断
            {
                printf("%d %.5lf\n",v2,c2);
                continue;
            }
            else
            {
                lay tmp=vis.top();
                vis.pop();
                v3=tmp.v;//结构体
                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.栈-洗盘子 nefu 1629

⭐️此题虽用三个栈,但其实并没有前两道题难⭐️

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);//提高速度
    stack <int> n0;
    stack <int> n1;
    stack <int> n2;//利用三个栈
    int n,i,j;
    int x,y;//1 洗 2 擦 个数
    //int tmp1,tmp2;
    cin>>n;
    for(i=n; i>=1; i--)
        n0.push(i);//未洗盘子顺序!!!注意摆放顺序,一在最上面
    //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;//注意要结束循环!!!
    }
    while (!n2.empty())
    {
        cout<<n2.top()<<endl;
        n2.pop();
    }
    return 0;
}

此篇为寒假集训完结篇,其实六号已经集训完成,六号下午有考试,所以上午未做完全部题,近日才写完。请期待下一篇 大一下ACM选修课 ❤️❤️❤️❤️

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值