杭电 oj 3350 #define is unsafe

#define is unsafe

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 568 Accepted Submission(s): 359

Problem Description
Have you used #define in C/C++ code like the code below?

#include <stdio.h>
#define MAX(a , b) ((a) > (b) ? (a) : (b))
int main()
{
printf(“%d\n” , MAX(2 + 3 , 4));
return 0;
}

Run the code and get an output: 5, right?
You may think it is equal to this code:

#include <stdio.h>
int max(a , b) { return ((a) > (b) ? (a) : (b)); }
int main()
{
printf(“%d\n” , max(2 + 3 , 4));
return 0;
}

But they aren’t.Though they do produce the same anwser , they work in two different ways.
The first code, just replace the MAX(2 + 3 , 4) with ((2 + 3) > (4) ? (2 + 3) : 4), which calculates (2 + 3) twice.
While the second calculates (2 + 3) first, and send the value (5 , 4) to function max(a , b) , which calculates (2 + 3) only once.

What about MAX( MAX(1+2,2) , 3 ) ?
Remember “replace”.
First replace: MAX( (1 + 2) > 2 ? (1 + 2) : 2 , 3)
Second replace: ( ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) > 3 ? ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) : 3).
The code may calculate the same expression many times like ( 1 + 2 ) above.
So #define isn’t good.In this problem,I’ll give you some strings, tell me the result and how many additions(加法) are computed.

Input
The first line is an integer T(T<=40) indicating case number.
The next T lines each has a string(no longer than 1000), with MAX(a,b), digits, ‘+’ only(Yes, there’re no other characters).
In MAX(a,b), a and b may be a string with MAX(c,d), digits, ‘+’.See the sample and things will be clearer.

Output
For each case, output two integers in a line separated by a single space.Integers in output won’t exceed 1000000.

Sample Input
6
MAX(1,0)
1+MAX(1,0)
MAX(2+1,3)
MAX(4,2+2)
MAX(1+1,2)+MAX(2,3)
MAX(MAX(1+2,3),MAX(4+5+6,MAX(7+8,9)))+MAX(10,MAX(MAX(11,12),13))

Sample Output
1 0
2 1
3 1
4 2
5 2
28 14

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;
struct asdf
{
    int tot,num;
}tem,in,out;
stack<asdf>st;//保存数据的 信息
stack<char>cs;//保存 运算符
int n,m;
char s[10005];
int main()
{
    int t;
    cin>>t;
    getchar();
    while(t--)
    {
        gets(s);
        int len=strlen(s);
        for(int i=0;i<len;i++)
        {
            if(s[i]>='0'&&s[i]<='9')
            {
                int te=0;
                while(s[i]<='9'&&s[i]>='0')//计算数字  值
                {
                    te=te*10+s[i]-'0';
                    i++;
                }
                if(!cs.empty()&&cs.top()=='+')
                {
                    tem=st.top();
                    st.pop();
                    tem.num+=te;//计算和
                    tem.tot++;//加法 加一
                    st.push(tem);
                    cs.pop();
                }
                else
                {
                    tem.num=te;//第一个直接入栈
                    tem.tot=0;//加法个数
                    st.push(tem);
                }
                i--;
            }
            else if(s[i]=='+'||s[i]=='(')
            {
                cs.push(s[i]);//直接入栈  方便计算加法个数   和和
            }
            else if(s[i]==')'||s[i]==',')//
            {//满足条件时候 向前运算 
                while(!cs.empty()&&cs.top()=='+')
                {
                    in=st.top();
                    st.pop();
                    out=st.top();
                    st.pop();
                    tem.tot=in.tot+out.tot+1;//多次加法 (⊙o⊙)
                    tem.num=in.num+out.num;//求和
                    st.push(tem);
                    cs.pop();//入栈
                }
                if(s[i]==')'&&st.size()>=2)
                {
                    in=st.top();
                    st.pop();
                    out=st.top();
                    st.pop();
                    tem.num=in.num+out.num;//求和
                    if(out.num>in.num)
                    {
                        out.tot=out.tot*2+in.tot;//根据题目要求统计和大的那个数的加法个数
                        st.push(out);//加法的个数*2  因为它MAX(MAX()) 等情况时间 MAX中的加法 是成倍增加的 
                    }
                    else
                    {
                        in.tot=in.tot*2+out.tot;
                        st.push(in);
                    }
                    cs.pop();
                }
            }
        }
        while(!cs.empty())//不为空 且没有MAX时间
        {
            while(!cs.empty()&&cs.top()=='+')//同上
            {
                in=st.top();
                st.pop();
                out=st.top();
                st.pop();
                tem.tot=in.tot+out.tot+1;//累加 加法个数
                tem.num=in.num+out.num;
                st.push(tem);
                cs.pop();
            }
        }
        cout<<st.top().num<<" "<<st.top().tot<<endl;
        st.pop();//清空栈   
    }
    return 0;
}
posted @ 2017-05-01 21:05  南风古  阅读(203)  评论(0编辑  收藏  举报