POJ 1472 Instant Complexity

又见神模拟题。。。。。

虽然处理起来感觉比较麻烦,但是思路还是比较清晰的,就是一个普通的栈。代码写的挫了一点....

注意 LOOP 0 的情况。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>

using namespace std;

struct N
{
    int ty,coe,ind;
}st[10000000];

bool cmp(N a,N b)
{
    if(a.ty == b.ty)
    {
        return a.ind > b.ind;
    }
    return a.ty < b.ty;
}

int main()
{
    int icase = 0;
    int T;
    int top;
    int i,j,len;
    int mark,w;
    char kw[20];
    scanf("%d",&T);
    while(T--)
    {
        top = 0;
        mark = 1;
        scanf("%s",kw);
        while(mark)
        {
             scanf("%s",kw);
             if(strcmp(kw,"LOOP") == 0)
             {
                 mark++;
                 scanf("%s",kw);
                 for(i = 0;kw[i] == ' '; ++i)
                    ;
                 if(kw[i] == 'n')
                 {
                    st[top].ty = 0;
                    st[top].coe = 1;
                    st[top].ind = 1;
                    top++;
                 }
                 else
                 {
                     for(w = 0,len = strlen(kw);i < len; ++i)
                     {
                         w += kw[i] - '0';
                         w *= 10;
                     }
                     w /= 10;
                     st[top].coe = w;
                     st[top].ind = 0;
                     st[top].ty = 1;
                     top++;
                 }
             }
             else if(strcmp(kw,"END") == 0)
             {
                 mark--;
                 if(mark == 0)
                    break;
                 for(i = top-1;i >= 0; --i)
                 {
                     if(st[i].ty <= 1)
                        break;
                 }

                for(j = i+1;j < top; ++j)
                {
                    st[j].ind += st[i].ind;
                    st[j].coe *= st[i].coe;
                }
                if(i+1 != top)
                    st[i].ty = 100;
                else st[i].ty = 2;
             }
             else if(strcmp(kw,"OP") == 0)
             {
                 scanf("%d",&w);
                 st[top].ty = 2;
                 st[top].coe = w;
                 st[top].ind = 0;
                 top++;
             }
        }

        for(i = 0;i < top; ++i)
        {
            if(st[i].coe == 0)
                st[i].ind = 0;
        }

        sort(st,st+top,cmp);


        for(i = 1;i < top && st[i].ty != 100; ++i)
        {
            if(st[i].ind == st[i-1].ind)
            {
                st[i].coe += st[i-1].coe;
                st[i-1].ty = 100;
            }
        }

        sort(st,st+top,cmp);

        printf("Program #%d\n",++icase);
        printf("Runtime = ");
        for(i = 0;i < top && st[i].ty != 100; ++i)
        {
            if(i != 0)
                printf("+");
            if(st[i].coe != 1 || st[i].ind == 0)
            {
                printf("%d",st[i].coe);
            }
            if(st[i].coe != 1 && st[i].ind != 0)
            {
                printf("*n");
            }
            if(st[i].coe == 1 && st[i].ind != 0)
            {
                printf("n");
            }
            if(st[i].ind > 1)
            {
                printf("^%d",st[i].ind);
            }
        }
        printf("\n\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值