#define is unsafe hdu3350

#define is unsafe

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


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
 
思路:
如果是数字就进栈,如果是加号或者是(代表运算的开始,也放入栈中,如果遇到了一个逗号,代表左边的已经输入完毕,要运算左边的值,如果遇到)代表max运算结束了,先把逗号右边的算出来,左边的值与右边的进行比较。进行完这一系列之后,如果运算符的栈里面还有值的话就是max之间的运算。
 
#include<stack>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string s;
struct number
{
	int a;int res;
	number(){
	}
	number(int x,int y){
		a=x;res=y;
	}
};
number x1,x2;
stack<number>q;
stack<char>oper;
int main()
{
	int ca;
	scanf("%d",&ca);
	while(ca--)
	{
		cin>>s;
		int cur=0;
		for(int i=0;i<s.length();i++)
		{
			if(s[i]>='0'&&s[i]<='9')
			{
				cur=s[i]-'0';
				i++;
				while(s[i]>='0'&&s[i]<='9')
				{
					cur=cur*10+s[i]-'0';
					i++;
				}
				i--;
				q.push(number(cur,0));
			}
			else if(s[i]=='+'||s[i]=='(')oper.push(s[i]);
			else if(s[i]==',')
			{
				while(!oper.empty()&&oper.top()=='+')
				{
					x1=q.top();q.pop();
					x2=q.top();q.pop();
					x1.a+=x2.a;
					x1.res+=(x2.res)+1;
					q.push(x1);
					oper.pop(); 
				}
			}
			else if(s[i]==')')//说明一个max运算结束 
			{
				while(!oper.empty()&&oper.top()=='+')
				{
					x1=q.top();q.pop();
					x2=q.top();q.pop();
					x1.a+=x2.a;
					x1.res+=(x2.res)+1;
					q.push(x1);
					oper.pop(); 
				}
				oper.pop();//弹出(
				x2=q.top();q.pop();
				x1=q.top();q.pop();
				if(x1.a>x2.a)//大的那个加法算了两次 
				{
					x1.res=x1.res*2+x2.res;
					q.push(x1);
				}
				else 
				{
					x1.res=x1.res+x2.res*2;
					x1.a=x2.a;
					q.push(x1);
				}
			}
		}
		while(!oper.empty()){//max之间的加法运算 
			x1=q.top();q.pop();
			x2=q.top();q.pop();
			x1.a+=x2.a;
			x1.res+=(x2.res+1);
			q.push(x1);
			oper.pop();
		}  
		printf("%d %d\n",q.top().a,q.top().res);
		q.pop();
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值