ACM Steps_Chapter Eight_Section1

ACboy needs your help again!

#include <queue>
#include <stack>
#include <string>
#include <iostream>
using namespace std;
int main()
{
       int t,n,temp,i;
       scanf("%d",&t);
       while(t--)
       {
              char str[5],str1[5];
              queue<int>Q;
              stack<int>S;
              scanf("%d",&n);
              scanf("%s",str);
              for(i=0;i<n;i++)
              {
                     if(str[2]=='F')
              {
                     scanf("%s",str1);
                     if(str1[0]=='I')
                     {
                            scanf("%d",&temp);
                            Q.push(temp);
                     }
                     if(str1[0]=='O')
                     {
                            if(Q.empty())
                                   printf("None\n");
                            else
                            {
                                   printf("%d\n",Q.front());
                                   Q.pop(); } } }
              else
              {
                     scanf("%s",str1);
                     if(str1[0]=='I')
                     {
                            scanf("%d",&temp);
                            S.push(temp);
                     }
                     if(str1[0]=='O')
                     {
                            if(S.empty())
                                   printf("None\n");
                            else
                            {
                                   printf("%d\n",S.top());
                                   S.pop();
                            }

                     }
              }
              }
       }
       return 0;
}

Train Problem I

#include <stdio.h>
#include <string.h>
int main()
{ int n,i,j,k,t,r[23],stack[13];
  char si[13],so[13];
  while(scanf("%d",&n)!=EOF)
  {
      getchar();
      memset(r,0,sizeof(r));
      memset(stack,0,sizeof(stack));
      scanf("%s%s",si,so);
      stack[1]=si[0];r[0]=1;
      for(k=t=j=1,i=0;so[i]!='\0';i++)
       {
          while(t<n)
          {
            if(j>0&&stack[j]==so[i]) //出栈
            {
               j--;
              r[k++]=2;//记录进出栈的情况
              break;
            }
            stack[++j]=si[t++];//进栈
            r[k++]=1; //记录进出栈的情况
          }
         if(stack[j]==so[i])//出栈
        {
            j--;
            r[k++]=2; //记录进出栈的情况
        }
     }

       n=n<<1;
       if(k<n)
         printf("No.\nFINISH\n");//这里开始比较郁闷的,No写成了大写还有下面的Yes,唉悲剧!
       else
         {   printf("Yes.\n");
             for(i=0;i<k;i++)
               switch(r[i])
               {
                   case 1:printf("in\n");break;
                   case 2:printf("out\n");break;
               }
             printf("FINISH\n");
         }
  }
  return 0;
}

简单计算器

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
    int i;
    double a,b;
    char s[250],c;
    while(gets(s),strcmp(s,"0")!=0)
    {
        stack<char>s1;
        stack<double>s2;
        for(i=0; s[i]; i++)
        {
            if(s[i]>='0'&&s[i]<='9')
            {
                a=0;
                while(s[i]>='0'&&s[i]<='9')
                {
                    a=a*10+s[i]-'0';
                    i++;
                }
                i--;
                s2.push(a);
            }
            else if(s[i]=='-'||s[i]=='+')
            {
                if(!s1.empty())
                {
                    c=s1.top();
                    s1.pop();
                    a=s2.top();
                    s2.pop();
                    b=s2.top();
                    s2.pop();
                    if(c=='+')
                        a+=b;
                    else
                        a=b-a;
                    s2.push(a);
                    s1.push(s[i]);
                }
                else
                    s1.push(s[i]);
            }
            else if(s[i]=='/' || s[i] == '*')
            {
                char ch = s[i];
                b=0;
                i+=2;
                while(s[i]>='0'&&s[i]<='9')
                {
                    b=b*10+s[i]-'0';
                    i++;
                }
                i--;
                a=s2.top();
                s2.pop();
                if(ch == '/')
                a=a/b;
                else
                a = a*b;
                s2.push(a);
            }
        }
        while(!s1.empty())
        {
            c=s1.top();
            s1.pop();
            a=s2.top();
            s2.pop();
            b=s2.top();
            s2.pop();
            if(c=='+')
                a+=b;
            else
                a=b-a;
            s2.push(a);
        }
        printf("%.2f\n",s2.top());
    }
    return 0;
}

看病要排队

#include <iostream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;
struct node
{
  int n;
  int i;
};
struct cmp
{
    bool operator ()(const node&a,const node&b)
    {
        if(a.n==b.n)
             return a.i>b.i;//开始没用写这个呀、、WA、、、囧
         else
             return a.n<b.n;
    }
};
int main()
{
    int n,A,B;
    node t;
    int k;
    char s[4];
    while(scanf("%d",&n)!=EOF)
    {
        k=1;
        priority_queue<node,vector<node>,cmp> Q[4];
        while(n--)
        {
           scanf("%s",s);
           if(s[0]=='I')
           {
               scanf("%d%d",&A,&B);
               t.n=B;
               t.i=k++;
               Q[A].push(t);
           }
           else
           {
               scanf("%d",&A);
               if(!Q[A].empty())
               {
                   t=Q[A].top();
                   Q[A].pop();
                   printf("%d\n",t.i);
               }
               else
               {
                   printf("EMPTY\n");
               }
           }
        }
    }
    return 0;
}

士兵队列训练问题

//必须每一轮都点完,而不是一数到只剩三人了就停止
#include <iostream>
using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        while(n--)
        {
            int m,a[5005] = {0},i;
            cin >> m;
            int sum;
            sum = m;
            for(i = 1; i<=m; i++)
            {
                a[i] = i;
            }
            if(m<=3)
            {
                cout << "1";
                for(i = 2; i<=m; i++)
                {
                    if(a[i])
                    {
                        cout << " " << a[i];
                    }
                }
                cout << endl;
                continue;
            }
            while(1)
            {
                int flag = 0;
                for(i = 1; i<=m; i++)
                {
                    if(a[i])
                        flag++;
                    if(flag==2)
                    {
                        a[i] = 0;
                        flag = 0;
                        sum--;
                    }
                }
                if(sum<=3)
                break;
                flag = 0;
                for(i = 1; i<=m; i++)
                {
                    if(a[i])
                        flag++;
                    if(flag==3)
                    {
                        a[i] = 0;
                        flag = 0;
                        sum--;
                    }
                }
                if(sum<=3)
                    break;
            }
            cout << "1";
            sum--;
            for(i = 2; sum; i++)
            {
                if(a[i])
                {
                    cout << " " << a[i];
                    sum--;
                }
            }
            cout << endl;
        }
    }

    return 0;
}

Windows Message Queue

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;
struct node
{
    char s[100];
    int n;
    int p;
    int i;
};
struct cmp
{
    bool operator ()(const node&a,const node&b)
    {
        if(a.p==b.p)
        {
            return a.i>b.i;
        }
        return a.p>b.p;
    }
};
int main()
{
    node t;
    priority_queue<node,vector<node>,cmp> Q;
    char s[5];
    int k=1;
    while(scanf("%s",s)!=EOF)
    {
        if(s[0]=='G')
        {
            if(!Q.empty())
            {
                t=Q.top();
                Q.pop();
                printf("%s %d\n",t.s,t.n);
            }
            else
              printf("EMPTY QUEUE!\n");//开始时这忘记写'!'符号,贡献了次WA
        }
        else
        {
           scanf("%s%d%d",t.s,&t.n,&t.p);
             t.i=k++;
           Q.push(t);
        }
    }
    return 0;
}

愚人节的礼物

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
#define N 1005
char s[N];
stack<char> tack;
int main()
{
   while(scanf("%s",s)!=-1)
   {
       while(!tack.empty())
    tack.pop();
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
       if(s[i]=='B')
    {
       printf("%d\n",tack.size());
    break;      
       }    
       if(s[i]=='(')
       tack.push(s[i]);
       if(s[i]==')')
       tack.pop();
    }        
   }
   return 0;  
}

Team Queue

#include<iostream>
#include<queue>
#include<string>
#include<map>
#pragma warning (disable:4786)
using namespace std;
int main()
{
    int i,n,m,t,ca=1;
    bool visit[1002];
    char str[20];
    while(scanf("%d",&n),n)
    {
        queue<int> q[1002],que;
        map<int,int> team;
        for(i=0;i<n;i++)
        {
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d",&t);
                team[t]=i;
            }
        }
        memset(visit,0,sizeof(visit));
        printf("Scenario #%d\n",ca++);
        while(scanf("%s",str))
        {
            if(strcmp(str,"STOP")==0)
            {
                puts("");
                break;
            }
            else if(strcmp(str,"ENQUEUE")==0)
            {
                scanf("%d",&t);
                q[team[t]].push(t);
                if(visit[team[t]]==false)
                {
                    que.push(team[t]);
                    visit[team[t]]=true;
                }
            }
            else
            {
                printf("%d\n",q[que.front()].front());
                q[que.front()].pop();
                if(q[que.front()].empty())
                {
                    visit[que.front()]=false;
                    que.pop();
                }    
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值