pku 1472 Instant Complexity

糾結的一道模擬題  ,快被它弄崩潰了,不做評論,棧模擬做的,

需要注意的地方有:注意LOOP的參數爲0的情況,然後輸出的格式相當複雜,但我的錯誤不是這兩個。   

 

我是錯在這種情況:

 

BEGIN LOOP n OP 1 END LOOP  n OP 1 END END

 

正確的答案應該是 2*n   我之前的結果好象是 n^2+n

 

也就是沒有考慮到LOOP 並行的情況,當成總是嵌套的來處理了。   這個錯誤弄了n久才發現,太糾結了。 

 

 

 

上代碼了:

 

 

#include <iostream>

#include <stack>

#include <cstring>

#include <cstdio>

using namespace std;

 

int x[15];

 

struct op

{

    string cmd;

    string ope;

    int s[15];

};

 

int main()

{

    int ca(1);

    int T;

    cin>>T;

    while (T--)

    {

        stack<op> a;

        string cmd;

        string ope;

        op tmp;

        memset(x,0,sizeof(x));

 

        cin>>cmd;

 

        tmp.cmd = cmd;

        a.push(tmp);

        while (!a.empty())

        {

            cin>>cmd;

            if (cmd!="END")

            {

                cin>>ope;

                tmp.cmd = cmd;

                tmp.ope = ope;

                a.push(tmp);

            }

            else

            {

                int value;

                while (true)

                {

 

                    op top = a.top();

                    a.pop();

                    if (top.cmd=="OP")

                    {

                        sscanf(top.ope.c_str(),"%d",&value);

                        x[0]+=value;

 

                    }

                    else if (top.cmd=="LOOP")

                    {

                        int k;

                        if (top.ope!="n")

                        {

                            sscanf(top.ope.c_str(),"%d",&value);

                            for (k=0;k<=10;++k)

                                x[k]*=value;

                        }

                        else

                        {

                            for (k=10;k>0;--k)

                                x[k]=x[k-1];

                            x[0]=0;

                        }

                        tmp.cmd = "temp";

                        for (k=0;k<=10;++k)

                        {

                            tmp.s[k] = x[k];

                            x[k] = 0;

                        }

                        a.push(tmp);

                        break;

                    }

                    else if (top.cmd=="temp")

                    {

 

                        for (int k = 0;k<=10;++k)

                            x[k] += top.s[k];

                    }

                    else break;

                }

            }

        }

 

                cout<<"Program #"<<ca++<<endl<<"Runtime = ";

               int zero(0);

                bool first(1);

                int j=10;

                while (j>=0)

                    zero+=x[j--];

                if (!zero)

                {

                    cout<<0<<endl<<endl;

                    continue;

                }

                j=10;

                while (j>=0)

                {

 

                    if (x[j])

                    {

                        if (!first) cout<<"+";

                        if (x[j]>1||j==0) cout<<x[j];

                        if (j>=1&&x[j]>1) cout<<"*";

                        if (j>=1) cout<<"n";

                        if (j>1) cout<<"^"<<j;

                        first = false;

                    }

                    --j;

                }

 

                cout<<endl<<endl;

 

     }

 

 

    return 0;

}

 

發佈了32 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章