HDU 3350 #define is unsafe 栈的模拟题 宏问题
来源:互联网 发布:win32 api 知乎 编辑:程序博客网 时间:2024/05/16 00:53
#define is unsafe
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 252 Accepted Submission(s): 152
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.
#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.
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
6MAX(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 02 13 14 25 228 14
/*hdoj 3350 栈的模拟题MAX(1+1,2)+MAX(2,3)读入整个字符串:遇到MAX跳过,1.遇到'(','+' 入符号栈 2.遇到数字 入数字栈 3,遇到','表示max的一半已经进入 判断符号栈顶是否为'+', 是,就要弹出2个数字操作+ 4,遇到')',表示max结束,就要操作比较大小 但是左边仍然有可能有+的操作 , 所以,先要判断符号栈顶是否为'+', 是,就要弹出2个数字操作+ 然后开始比较大小: 注意这里默认的','前后的+操作为0, 若有一方为1,且是比较大的数,则操作是需要乘以2的 MAX(1+2,2)==>max(3,1)==>3 操作了2次+,同下: ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 最后结束 仍然要判断符号栈顶是否有+操作,就是max中间的+ */#include<iostream>#include<stdio.h>#include<string.h>#include<stack>using namespace std;struct number{ int x,count;};stack<number> num_stack;stack<char> oper;char str[1001];int main(){ int t,len,i,y; number temp1,temp2,num; // freopen("test.txt","r",stdin); scanf("%d",&t); while(t--) { scanf("%s",str); len=strlen(str); for(i=0;i<len;i++) { if(str[i]>='0'&&str[i]<='9') //数字 提取完整入数字栈 { y=str[i]-'0'; i++; while(str[i]>='0'&&str[i]<='9') { y=y*10+str[i]-'0'; i++; } i--; num.count=0; num.x=y; num_stack.push(num); } else if(str[i]=='('||str[i]=='+')//( ,+ 符号 直接入符号栈 { oper.push(str[i]); } else if(str[i]==',')//如果是‘,’,则把‘,’左边连续的‘+’进行运算, //运算结果重新压入栈中 { while(!oper.empty()&&oper.top()=='+') { temp1=num_stack.top(); num_stack.pop(); temp2=num_stack.top(); num_stack.pop(); temp1.x+=temp2.x; temp1.count+=temp2.count+1; num_stack.push(temp1); oper.pop(); } } else if(str[i]==')')//遇到‘)’,则说明有一个MAX 可以执行, //但要先把左边连续的‘+’运算完 { while(!oper.empty()&&oper.top()=='+') { temp1=num_stack.top(); num_stack.pop(); temp2=num_stack.top(); num_stack.pop(); temp1.x+=temp2.x; temp1.count+=temp2.count+1; num_stack.push(temp1); oper.pop(); } oper.pop(); temp2=num_stack.top(); num_stack.pop(); temp1=num_stack.top(); num_stack.pop(); if(temp1.x>temp2.x) temp1.count=temp1.count*2+temp2.count; else { temp1.x=temp2.x; temp1.count=temp1.count+temp2.count*2; } num_stack.push(temp1); } } while(!oper.empty()&&oper.top()=='+')//完之后,还可能存在操作符没运算, //但只可能是‘+’ { temp1=num_stack.top(); num_stack.pop(); temp2=num_stack.top(); num_stack.pop(); temp1.x+=temp2.x; temp1.count+=temp2.count+1; num_stack.push(temp1); oper.pop(); } printf("%d %d\n",num_stack.top().x,num_stack.top().count); num_stack.pop(); } return 0;}
0 0
- HDU 3350 #define is unsafe 栈的模拟题 宏问题
- HDU 3350 #define is unsafe 栈的模拟题
- hdu 3350 #define is unsafe
- May_Day训练赛模拟题 HDOJ 3350 #define is unsafe
- HDU3350 #define is unsafe 栈的应用
- HDU-3350 #define is unsafe(dfs 解析字符串)
- 【题解】HDU.3350.#define is unsafe
- 杭电 oj 3350 #define is unsafe
- #define is unsafe
- #define is unsafe hdu3350
- #define is unsafe
- define 宏引起的问题
- fopen is unsafe 错误
- HDU 1084 What Is Your Grade? (模拟题)
- #define 引发的问题
- typedef & #define的问题
- JSLINT 的奇怪问题 Unsafe character.
- 解决VS中function unsafe的问题
- Type Qualifier (GLSL)--GLSL类型修饰词
- POJ-2983-Is the Information Reliable?(差分约束)
- 跨战区模式PK(总结)
- 在C和C++定义最大最小整数值
- POJ 2983 Is the Information Reliable?(差分约束第一发)
- HDU 3350 #define is unsafe 栈的模拟题 宏问题
- Generate Parentheses
- python搭建web server
- 行为类模式大PK(总结)
- 一个 Linux 上分析死锁的简单方法
- HDU 3290 The magic apple tree DFS
- 剑指offer面试题31:连续子数组的最大和
- 变脸不变质的桥梁模式(Bridge Pattern)
- hdu 4135 Co-prime (组合数学:容斥定理+欧拉函数)