c123: 00514 - Rails

https://zerojudge.tw/ShowProblem?problemid=c123


AC

//By SCJ
//uva 514
#include<bits/stdc++.h>
using namespace std;
int main()
{

    int n;
    bool fir=0;
    while(cin>>n,n!=0)
    {
        while(1)
        {
            bool fg=0;
            int t,cnt=2;
            stack<int> s;
            s.push(1);
            bool ok=1;//預設是ok

            //
            for(int i=0;i<n;++i)
            {
                cin>>t;//輸入每個車廂

                if(t==0){//輸入0代表造跳出
                    fg=1;
                    break;
                }

                while(cnt<=n&&(s.empty()||s.top()!=t))
                {
                    s.push(cnt++);
                }

                if(s.empty()||s.top()!=t){
                    //不ok
                    ok=0;
                }
                else {
                        s.pop();
                     }
            }

            //輸入0所以跳出
            if(fg) break;

            if(ok) {
                cout<<"Yes"<<endl;
            }else{
                cout<<"No"<<endl;
            } 
        }
        cout<<endl;
    }
}

Allen翻譯版本


//uva 514
#include<bits/stdc++.h>
using namespace std;
int main()
{

    //n代表要輸入幾個火車廂
    int n;
    while(cin>>n,n!=0)
    {
        while(1)
        {
            bool flag=0;//flag=1代表要跳出目前的測試模式
            int train;
            int cnt=2;//代表要駛入車站的車廂號碼
            stack<int> stk;//火車站

            stk.push(1);//先讓第一節車廂開進火車站
            bool ok=true;//預設是ok,預設是會成功的,中途遇到錯誤再改成ok=false

            for(int i=0;i<n;++i)
            {
                cin>>train;//輸入每個車廂

                if(train==0){//輸入0代表要跳出
                    flag=1;
                    break;
                }

                //還有未進站的火車 且 目前車站的車頭不是我們要的
                while(cnt<=n&&(stk.empty()||stk.top()!=train))
                {
                    //if(stk.empty())cout<<"車站沒車"<<endl;
                    //cout<<"火車("<<cnt<<")進站"<<endl;
                    stk.push(cnt++);
                }

                //如果車站是空的 或 要出站的火車不是我們要的
                if(stk.empty()||stk.top()!=train){
                    //代表出車失敗,ok = false
                    ok=false;
                }
                else {
                        //成功出車,把火車送出停靠站,往B前進
                        //cout<<"火車("<<stk.top()<<")出站"<<endl;
                        stk.pop();
                     }
            }

            //輸入0所以跳出
            if(flag) break;

            if(ok) {
                cout<<"Yes"<<endl;
            }else{
                cout<<"No"<<endl;
            }
        }
        cout<<endl;
    }
}

results matching ""

    No results matching ""