Light OJ 1113

 WA了好多次,注意的是进出栈的先后顺序,还有相关的判断。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
typedef struct wuliao
{
    char s[60];
}ss;
stack<ss> Back,For;
char flag[10];
ss url,tmp;
void unit()
{
    while(!Back.empty())
        Back.pop();
    while(!For.empty())
        For.pop();
}
int main()
{
    int T,t;
    cin>>T;
    for(t=1;t<=T;t++)
    {
       unit();
       strcpy(tmp.s,"http://www.lightoj.com/");
       cout<<"Case "<<t<<":"<<endl;
       while(~scanf("%s",flag)&&strcmp(flag,"QUIT")!=0)
       {
           if(strcmp(flag,"VISIT")==0)
           {
              Back.push(tmp);
              cin>>tmp.s;
              cout<<tmp.s<<endl;
              while(For.size())
                    For.pop();
           }
           else if(strcmp(flag,"BACK")==0)
           {
             if(Back.empty())
                cout<<"Ignored"<<endl;
             else
             {
                 For.push(tmp);
                 tmp=Back.top();
                 Back.pop();
                 cout<<tmp.s<<endl;
             }
          }
          else if(strcmp(flag,"FORWARD")==0)
          {
             if(For.empty())
                cout<<"Ignored"<<endl;
             else
             {
                 Back.push(tmp);
                 tmp=For.top();
                 For.pop();
                 cout<<tmp.s<<endl;
             }
          }
          else continue;
       }
    }
    return 0;
}

推荐一个写的比较漂亮的代码:

 #include <cstdio>  
    #include <iostream>  
    #include <string>  
    #include <stack>  
    #include <algorithm>  
    using namespace std;  
    int main()  
    {  
        int t;  
        scanf("%d",&t);  
        for(int i=1;i<=t;i++)  
        {  
            stack<string>F,B;  
            string str="http://www.lightoj.com/",command;  
            printf("Case %d:\n",i);  
            while(cin>>command&&command!="QUIT")  
            {  
                if(command=="BACK")  
                {  
                    if(B.empty())  
                        printf("Ignored\n");  
                    else  
                    {  
                        F.push(str);  
                        str=B.top();  
                        B.pop();  
                        cout<<str<<endl;  
                    }  
                }  
                else if(command=="FORWARD")  
                {  
                    if(F.empty())  
                        printf("Ignored\n");  
                    else  
                    {  
                        B.push(str);  
                        str=F.top();  
                        F.pop();  
                        cout<<str<<endl;  
                    }  
                }  
                else  
                {  
                    B.push(str);  
                    cin>>str;  
                    cout<<str<<endl;  
                    while(F.size())  
                        F.pop();  
                }  
            }  
        }  
        return 0;  
    }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值