I. #define is unsafe

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(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b*2次。
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
struct state{
state(int a,int b){s=a,k=b;}
int s,k; //s为和,k为次数
};
state find(string str){
int in=,len=str.length(),num=;//当前位置,字符串长度,和 if(str[in]>=''&&str[in]<=''){ //第一个字母是整数,读取这个数
while(in<len&&str[in]>=''&&str[in]<='')num=num*+str[in++]-'';
if(in>=len)return state(num,);//如果只剩一个数,直接返回
else{
state st=find(str.substr(in+));
return state(num+st.s,st.k+);
}
}else if(str[in]=='M'){
in+=;
int cnt=,mid=;
while(cnt>){ //匹配MAX()右括号的位置,并找出对应这个MAX的逗号的位置
if(str[in]=='(')cnt++;
else if(str[in]==')')cnt--;
if(str[in]==','&&cnt==)mid=in;
in++;
}
state le=find(str.substr(,mid-)); //求MAX(A,B)中的A
state ri=find(str.substr(mid+,in-mid-)); //求MAX(A,B)中的B
int p1,p2;
if(le.s>ri.s){
p1=le.s;
p2=le.k*+ri.k;
}else{
p1=ri.s;
p2=le.k+ri.k*;
}
if(in>=len-){ //已经到达末端
return state(p1,p2);
}else{ //MAX(A,B) + C的形式,求C
state st=find(str.substr(in+)); //处理加号后面的部分
return state(p1+st.s,st.k+p2+);
}
}
}
int main(){
int cas;
char str[];
cin>>cas;
while(cas--){
cin>>str;
state rs=find(str);
cout<<rs.s<<" "<<rs.k<<endl;
}
return ;
}
												

#define is unsafe——I的更多相关文章

  1. #define is unsafe

    #define is unsafe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  2. HDU 3350 #define is unsafe

    题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...

  3. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  4. Unity3D 使用C#指针unsafe

    Unsafe code requires the `unsafe' command line option to be specified 在Unity开发中,如果涉及到指针的使用,需要自己定义预处理 ...

  5. Unity3d中C#使用指针(Unsafe)的办法(转)

    近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...

  6. Visual Studio 2015 编译错误【错误 C4996 'vsprintf': This function or variable may be unsafe. Consider using vsprintf_s instead. 】的解决方案

    错误提示信息: 错误 C4996 'vsprintf': This function or variable may be unsafe. Consider using vsprintf_s inst ...

  7. This function or variable may be unsafe. Consider using scanf_s instead.

    去掉安全检查,开头加上即可: #define _CRT_SECURE_NO_WARNINGS 或者: 严重性代码 说明项目文件行禁止显示状态 错误C4996 'scanf': This functio ...

  8. 删除: warning C4996: &#39;sprintf&#39;: This function or variable may be unsafe. Consider 方法

    可以使用的最简单的方法: 选项Project   |   Configuration   Properties   |   C/C++   |   Preprocessor   |   Preproc ...

  9. Java中Unsafe类详解

    http://www.cnblogs.com/mickole/articles/3757278.html Java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操 ...

随机推荐

  1. 来访统计的JS代码

    <script language="JavaScript"> var caution = false function setCookie(name, value, e ...

  2. C#从Image上读取文本

    今天通过C#来实现一个读取Image上文本的功能. 1. 环境准备: 1). 下载 Microsoft Office SharePoint Designer 2007. 2). 安装请参考KB:htt ...

  3. jQuery Mobile Datepicker 使用

    插件引入文件: <meta name="viewport" content="width=device-width, initial-scale=1"&g ...

  4. Salesforce中所有常用类型字段的取值与赋值

    Salesforce中所有常用字段类型的定义以及如何用代码进行取值和赋值: Field Type的定义: http://www.salesforce.com/us/developer/docs/api ...

  5. loj 1316(spfa预处理+状压dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27024 题意:求0-(n-1)的经过最多的标记的点的最短路. 思路 ...

  6. STL中容器的push()或者push_back()函数的一点说明

    在STL的queue 或者 vector.list等容器适配器或者容器中,会经常用到的函数就是push()或者push_back()函数,但是有一点需要明确的是: 在使用这些函数对容器/适配器对象增加 ...

  7. Liferay 6.2 改造系列之十:修改系统登录相关配置

    1.关闭自动登录功能: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true to allo ...

  8. 2016.9.1 JavaScript入门之五

    1.数据类型:对象:也可以被认为是一个键/值存储,像一个字典.可以取代switch{case:case:}或者if else 例如: function phoneticLookup(val) { va ...

  9. [转] FastMM使用详解

    FastMM使用详解 一.引言      FastMM 是适用于delphi的第三方内存管理器,在国外已经是大名鼎鼎,在国内也有许多人在使用或者希望使用,就连 Borland 也在delphi2007 ...

  10. 本BLOG简介(内有一道UVa524素数环进阶版)【B001】

    [B001]Hi,大家好,今天我的博客第一天开通,今天奉上开博题,出自首都师师范大学附属中学OJ(题号未知在练习场中)原题为UVa524,题目要求如下: [难度B]—————————————————— ...