#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. Nodejs最好的ORM - TypeORM

    TypeORM是一个采用TypeScript编写的用于Node.js的优秀ORM框架,支持使用TypeScript或Javascript(ES5, ES6, ES7)开发.目标是保持支持最新的Java ...

  2. oracle 数据字典和动态性能视图

    一.概念数据字典是oracle数据库中最重要的组成部分,它提供了数据库的一些系统信息.动态性能视图记载了例程启动后的相关信息. 二.数据字典1).数据字典记录了数据库的系统信息,它是只读表和视图的集合 ...

  3. Sql Server——数据的增删改

    所谓数据的增删改就是在创建好数据库和表后向表中添加数据.删除表中的数据.更改表中的一些数据. 新增数据: 语法一: insert into 表名 values (数据内容)        --这里需要 ...

  4. 深入理解计算机系统chapter2

    ---恢复内容开始--- 整数表示: 反码和原码都会有正零和负零 有符号整数和无符号整数之间的转换 反之 扩展一个数字的位级表示 截断操作 无符号加法的益处 补码的加法 规格化的值:E=e-bias ...

  5. window、linux系统与linux服务器之间使用svn同步及自动部署代码的方法

    摘要: 在家用PC,在公司用办公电脑对一个项目的代码进行修改时,会遇到代码同步的问题.本文讲解了代码同步及自动部署的解决办法. 实现方法: 1.首先在linux服务器上和linux上安装svn(sud ...

  6. 《HelloGitHub》第 18 期

    <HelloGitHub>第 18 期 兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程. ...

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

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

  8. Spring Boot-------JPA——EntityManager构建通用DAO

    EntityManager EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满 ...

  9. jQuery实现web页面固定列表搜索

    1.需求分析:现在有一个数据展示列表页面,列表内容固定,使用jQuery在固定的列表中实现搜索功能. 2.核心代码: <!-- 添加jquery库 --> <script type= ...

  10. Python自学笔记-sorted()函数(来自廖雪峰的官网Python3)

    感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. 排序算法 排序 ...