poj 1686

来源:互联网 发布:东北大学iptv网络直播 编辑:程序博客网 时间:2024/06/10 23:20

这个题真是简直了。。。。。

不得不说java智能了。。恩

#include <cstdio>#include <cstring>#include <stack>#include <stdlib.h>#include<ctype.h>using namespace std;const int maxn=int (1e5)+5;stack <int> num;stack <char> op;int priv[127],val[27];void init(){    while(!num.empty())      num.pop();    priv['+']=priv['-']=3;    priv['*']=priv['/']=2;    priv['(']=10;//特殊处理}int calc(int a,int b,char op){    if(op=='+')        return a+b;    if(op=='-')        return a-b;    if(op=='*')        return a*b;}void calc(){    int y=num.top();    num.pop();    int x=num.top();    num.pop();    char tmpop=op.top();    op.pop();    num.push(calc(x,y,tmpop));}int calc(char *str){    init();    int x,y,i,n=strlen(str);    char tmpop,last=0;    for(i=0;i<n;i++)    {        if(isalpha(str[i]))            num.push(str[i]);        else            if(isdigit(str[i]))        {            num.push(atoi(str+i));            for(;i+1<n&&isdigit(str[i+1]);++i);        }        else            if(str[i]=='(')               op.push('(');        else            if(str[i]==')')        {            while(op.top()!='(')                calc();            op.pop();        }        else            if(str[i]=='-'&&(last==0||last=='('))        {            num.push(0);            op.push('-');        }        else            if(priv[str[i]])        {            while(!op.empty()&&priv[op.top()]<=priv[str[i]])                calc();            op.push(str[i]);        }        else continue;        last=str[i];    }    while(!op.empty())        calc();    return num.top();}char s[85],s2[85];bool ok(){   gets(s);   gets(s2);   return calc(s)==calc(s2);}int main(){    int t;    scanf("%d",&t);    getchar();    while(t--)    {        if(ok())            printf("YES\n");        else            printf("NO\n");    }    return 0;}

没有听到程学长讲题,理解这个题还是有了一点困难【因为比较繁琐

几乎是比着程学长的代码敲的啊喂才明白这个题的。。。智商确实拙计。可爱的程学长的博客如下啦:

http://endless.logdown.com/posts/2014/05/15/poj-1686-lazy-math-instructor-in-java-scriptengine-usage

大体思路就是用两个栈存数和运算符,-号时存储数的栈push一个0。如果运算符的栈top是)则进行计算直到碰到(。

把a,b什么的都转化成ASCⅡ码【或者什么数的】然后比较两个表达式的计算值。


0 0
原创粉丝点击