非常简单的题目,写了没有半小时,调试几分钟就对了,比赛是愣是没写。也不知怎么搞的。
#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;
}
1784

被折叠的 条评论
为什么被折叠?



