2020年 1月6日 OJ习题【栈】

栈-程序员输入问题

#include <bits/stdc++.h>
using namespace std;

int main()
{
    stack<char>x1;
    stack<char>x2;
    char str[110];
    gets(str);
    int l=strlen(str);
    for(int i=0;i<l;i++)
    {
        if(str[i]=='@')
        {
            while(!x1.empty())
            {
                x1.pop();//循环直到清空栈//
            }
            continue;
        }
        if(str[i]=='#')
        {
            if(!x1.empty())
                x1.pop();//清一个即可//
            continue;
        }
        else
            x1.push(str[i]);
    }
    while(!x1.empty())
    {
        x2.push(x1.top());
        x1.pop();
    }
    while(!x2.empty())
    {
        printf("%c",x2.top());
        x2.pop();
    }
    printf("\n");
    return 0;
}

栈-溶液模拟器

注意溶液浓度的计算方式

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    stack<int>x1;
    stack<int>x2;
    int v,n,v1;
    double c,c1;
    char ch;
    cin>>v>>c>>n;
    while(n--)
    {
        cin>>ch;
        if(ch=='P')
        {
            cin>>v1>>c1;
            x1.push(v1);
            x2.push(c1);
            c=(v*c+v1*c1)/(v+v1);
            v+=v1;
            printf("%d %.5lf\n",v,c);
        }
        else if(ch=='Z')
        {
            if(!x1.empty()||!x2.empty())
            {
                c=(v*c-x1.top()*x2.top())/(v-x1.top());
                v-=x1.top();
                x1.pop();
                x2.pop();
                printf("%d %.5lf\n",v,c);
            }
            else
                printf("%d %.5lf\n",v,c);//如果空了,要把原始数据输出//
        }
    }
    //cout << "Hello world!" << endl;
    return 0;
}

栈-火车编组

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    stack<int>x;
    int n,a[110],j=1;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    x.push(j);
    j++;
    printf("A");
    for(int i=0;i<n;i++)
    {
        while(a[i]!=x.top())
        {
            if(j==n+1)
            break;
            x.push(j);
            j++;
            printf("A");
        }
        if(a[i]==x.top())
        {
            x.pop();
            printf("B");
        }
        while(x.empty())//必须判断栈是否空,空了要继续入栈,否则上两个语句走不了//
        {
            if(j==n+1)
            break;
            x.push(j);
            j++;
            printf("A");
        }
    }
    printf("\n");
    //cout << "Hello world!" << endl;
    return 0;
}

栈-洗盘子

#include <bits/stdc++.h>
using namespace std;

int main()
{
     ios::sync_with_stdio(false);
     stack<int>x;
     stack<int>x1;
     stack<int>x2;
     int n,m,c;
     cin>>n;
     for(int i=n;i>=1;i--)
        x.push(i);
     while(cin>>m>>c)
     {
         if(m==1)
         {
             for(int j=1;j<=c;j++)
             {
                 x1.push(x.top());
                 x.pop();
             }
         }
         if(m==2)
         {
             for(int j=1;j<=c;j++)
             {
                 x2.push(x1.top());
                 x1.pop();
             }
         }
         if(x.empty()&&x1.empty())
            break;
     }
     while(!x2.empty())
     {
         printf("%d\n",x2.top());
         x2.pop();
     }
    //cout << "Hello world!" << endl;
    return 0;
}

栈-括号匹配

不懂的时候,拿一组数据实际模拟一下即可!!!

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    stack<char>x;
    char str[260],tmp;
    scanf("%s",str);
    int l=strlen(str);
    /*while(!x.empty())
    {
        x.pop();
    }*/
    for(int i=0;i<l;i++)
    {
        if(x.empty()) x.push(str[i]);//空的时候要入栈//
        else
        {
            tmp=x.top();//记录栈顶元素//
            if((tmp=='['&&str[i]==']')||(tmp=='('&&str[i]==')'))//只有该条件下才出栈//
            {
                x.pop();
            }
            else
                {
                    x.push(str[i]);
                }
        }
    }
    if(x.empty())
      cout<<"OK"<<endl;
    else
      cout<<"Wrong"<<endl;
    //cout << "Hello world!" << endl;
    return 0;
}

栈-表达式求值

不用栈即可求解!!!

#include <bits/stdc++.h>
using namespace std;

const int mod=1e4;
int main()
{
    char ch;
    int x,ans=0,tmp;
    scanf("%d",&x);
    tmp=x%mod;
    while(scanf("%c",&ch)&&ch!='\n')
    {
        scanf("%d",&x);
        x=x%mod;
        if(ch=='*') tmp=tmp*x%mod;//*或+其实判断前面算出来的数用不用在这一步加和,如果是+,就应该在这一步加和。如果是*,说明刚刚输入的数是需要与上一个数相乘的,因此不能直接加和!!!//
        else
        {
            ans=ans+tmp%mod;
            tmp=x%mod;
        }
    }
    ans=ans+tmp%mod;
    printf("%d\n",ans%mod);
    //cout << "Hello world!" << endl;
    return 0;
}

发布了10 篇原创文章 · 获赞 1 · 访问量 141

猜你喜欢

转载自blog.csdn.net/shiroi2333/article/details/103853947
今日推荐