nyoj 373 (时间复杂度)

非常简单的题目,写了没有半小时,调试几分钟就对了,比赛是愣是没写。也不知怎么搞的。

#include<stdio.h>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
char s[100005];
stack<char>s1;
stack<double>s2;
int main()
{
    //freopen("Input.txt","r",stdin);
    int n,ncase,i;
    while(~scanf("%d%d",&n,&ncase))
    {
        while(ncase--)
        {
            while(!s1.empty()) s1.pop();
            while(!s2.empty()) s2.pop();
            scanf("%s",s);
            double m=n*1.0,k;
            int len=strlen(s);
            int flag=0;
            for(i=0;i<len;i++)
            {
                if(s[i]=='q'||s[i]=='r'||s[i]=='t') continue;
                if(s[i]=='g'||s[i]=='o')  continue;
                if(s[i]=='l') flag=1;
                if(s[i]=='s') flag=2;
                if(s[i]=='n') s2.push(m);
                if(s[i]=='(')
                {
                    if(flag==1) { s1.push('l');flag=0;}  //把log 和sqrt 当成左括号。
                    else if(flag==2)  { s1.push('s');flag=0;}
                    else s1.push(s[i]);
                }
                if(s[i]=='*') s1.push(s[i]);
                if(s[i]==')')  //遇到右括号就计算到左括号,
                {
                    double a1,a2;
                    while(1)
                    {
                        char x=s1.top(); s1.pop();
                        if(x=='(') break;
                        a1=s2.top(); s2.pop();
                        if(x=='l')
                        {
                            k=log(a1)/log(2);
                            s2.push(k);
                            break;
                        }
                        if(x=='s')
                        {
                            k=sqrt(a1);
                            s2.push(k);
                            break;
                        }
                        if(x=='*')
                        {
                            a2=s2.top();s2.pop();
                            k=a1*a2;
                            s2.push(k);
                        }
                    }
                }
            }
            double t=s2.top();
            if(t>100000000) puts("TLE");
            else printf("%.2lf\n",t);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值