#define is unsafe

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 66 Accepted Submission(s): 52
 
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
 
Author
madfrog
 
Source
HDU2010省赛集训队选拔赛(校内赛)
 
Recommend
lcy
 
/*
题意:模拟题,给你一行只有MAX函数,和加法运算的表达式,输出表达式的值,并且统计在本质MAX运算中+运算的次数 初步思路:一行表达式中有用的有 (:表示一个运算开始了 , +:表示有一个加法运算, ):表示一个运算结束了 */
#include<bits/stdc++.h>
using namespace std;
struct node{
int a;//用来存储数字
int res;//用来存储加法运算的次数
node(){};
node(int b,int c){
a=b;
res=c;
}
};
stack<node>Q;//用来存储数字
stack<char>oper;//用来存储运算符
node x1,x2;
int n;
char str[];
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&n);
getchar();
while(n--){
gets(str);
//cout<<str<<endl;
for(int i=;i<strlen(str);i++){
if(str[i]>=''&&str[i]<=''){//如果当前位置的是数字的话,直接将他压进栈中
int cur=str[i]-'';
i++;
while(str[i]>=''&&str[i]<=''){
cur=cur*+str[i]-'';
i++;
}
i--;//这位不是数字了但是不能继续往前走,要让for循环来承担这个任务
Q.push(node(cur,));//将这个数字装进栈中,并且初始化经过乘法运算的次数为零
}else if(str[i]=='+'||str[i]=='('){
oper.push(str[i]);
}else if(str[i]==','){//如果遇见','就要将','左边的加法运算进行计算之后重新压到栈中
while(!oper.empty()&&oper.top()=='+'){//提取到一个加号就要运算一次
x1=Q.top();
Q.pop();
x2=Q.top();
Q.pop();
x1.a+=x2.a;
x1.res+=(x2.res+);//这地方将max函数展开看就知道为什么加法运算是累加了
Q.push(x1);
oper.pop();//将这个运算符出栈
}
}else if(str[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+);//这地方将max函数展开看就知道为什么加法运算是累加了
Q.push(x1);
oper.pop();//将这个运算符出栈
}
oper.pop();
x2=Q.top();
Q.pop();
x1=Q.top();//后取的才是第一个
Q.pop();
if(x1.a>x2.a){//说明x1的部分要运算两次
x1.res=x1.res*+x2.res;
Q.push(x1);
}else{//否则的话就是x2的部分要运算两次
x1.res=x1.res+x2.res*;
x1.a=x2.a;
Q.push(x1);
}
}
}
//将所有的操作进行之后,栈中如果还有运算,只能是MAX函数之间的加法运算
while(!oper.empty()){
x1=Q.top();
Q.pop();
x2=Q.top();
Q.pop();
x1.a+=x2.a;
x1.res+=(x2.res+);
Q.push(x1);
oper.pop();
}
printf("%d %d\n",Q.top().a,Q.top().res);
Q.pop();//将最后一个元素出栈
}
return ;
}

最新文章

  1. 51Nod-1136 欧拉函数
  2. log4net入门
  3. Swift实战之2048小游戏
  4. JavaScript入门篇 第二天(消息对话框+网页弹出)
  5. PS网页设计教程XXIX——如何在PS中设计一个画廊布局
  6. url组成部分
  7. Yii 框架表单验证---实例
  8. 第一部分 CLR基础:第3章 共享程序集和强命名程序集
  9. Java 声明和访问控制(三) finalize方法 成员访问修饰符
  10. Android实现通过手机找回密码
  11. 回滚 rollback
  12. 浅谈JavaScript时间与正则表达式
  13. android网络监测
  14. shader之半兰伯特漫反射
  15. Android Studio 字体和字号调整
  16. strace命令【转】
  17. Windows远程桌面卡的解决办法
  18. Android开发——打造简单的Viewpager指示器(小圆点指示器)
  19. python3 正则表达式点星问号(.*?)能不能匹配换行符?不能的话应该怎么写
  20. pyqt5 点开小窗口

热门文章

  1. AngularJS -- 指令(创建自定义指令)
  2. 使用paramiko远程执行
  3. 深入浅出AQS之条件队列
  4. SiganlR 系列之概述
  5. S2_SQL_第二章
  6. Python s12 Day3 笔记及作业
  7. express简介
  8. Ubuntu Docker 版本的更新与安装
  9. 编译LNMP之nginx+php-fpm
  10. Delphi实现电脑端微信图片文件解密