#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 ;
}

#define is unsafe的更多相关文章

  1. #define is unsafe——I

    I. #define is unsafe Have you used #define in C/C++ code like the code below? #include <stdio.h&g ...

  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. 分享一个图片上传插件(TP5.0)

    效果预览图: 该插件主要功能是:可预览裁剪图片和保存原图片,执行裁剪图片后会删除 裁剪的原图片目录,以便减少空间.一.下载附件 地址:https://pan.baidu.com/s/1bpxZhM3 ...

  2. DOM中document对象的常用属性方法

    每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 属性 1  document.anchors  返 ...

  3. windows7下MongoDB(V3.4)的使用及仓储设计

    简单的介绍一下,我使用MongoDB的场景. 我们现在的物联网环境下,有部分数据,采样频率为2000条记录/分钟,这样下来一天24*60*2000=2880000约等于300万条数据,以后必然还会增加 ...

  4. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  5. JSP入门 导出文件

    1.图片校验码 <img  src="captcha.jpg"  /> web.xml配置 <servlet>      <servlet-name& ...

  6. UI自动化测试(三)对页面中定位到的元素对象做相应操作

    前两天分别讲述了UI自动化测试基础以及对页面元素该如何进行定位,这一篇自然就是对定位到的页面元素对象进行相应操作啦. 阅读目录 1.常用操作元素对象的方法 2.鼠标事件操作 3.键盘事件操作 4.We ...

  7. MMORPG战斗系统随笔(三)、AI系统简介

    在设计一款游戏的时候,如果我们是玩家,是希望自己能够操作角色畅玩游戏的.在一款MMORPG游戏中,大部分的实际游戏角色,是需要玩家来操作的,通过在游戏大世界相互完成游戏中的任务等等来体验游戏.在大世界 ...

  8. sql的存储过程使用详解--基本语法

    存储过程简介 SQL语句需要先编译然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储 ...

  9. Centos7下安装php7

    通过编译的方式安装php7 1. 安装PHP7 ## 下载 wget http://us2.php.net/distributions/php-7.0.2.tar.gz ## 安装 tar zxvf ...

  10. ABP增删改查代码片段

    @using System.Web.Optimization @using MultiPageSimpleTask.Entitys.Dtos; @model IList<ProductDto&g ...