#define is unsafe——I
I. #define is unsafe
#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 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
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的更多相关文章
- #define is unsafe
#define is unsafe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3350 #define is unsafe
题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- Unity3D 使用C#指针unsafe
Unsafe code requires the `unsafe' command line option to be specified 在Unity开发中,如果涉及到指针的使用,需要自己定义预处理 ...
- Unity3d中C#使用指针(Unsafe)的办法(转)
近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...
- 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 ...
- This function or variable may be unsafe. Consider using scanf_s instead.
去掉安全检查,开头加上即可: #define _CRT_SECURE_NO_WARNINGS 或者: 严重性代码 说明项目文件行禁止显示状态 错误C4996 'scanf': This functio ...
- 删除: warning C4996: 'sprintf': This function or variable may be unsafe. Consider 方法
可以使用的最简单的方法: 选项Project | Configuration Properties | C/C++ | Preprocessor | Preproc ...
- Java中Unsafe类详解
http://www.cnblogs.com/mickole/articles/3757278.html Java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操 ...
随机推荐
- ubuntu下整合eclipse和javah生成jni头文件开发android的native程序(转)
本文介绍两种利用javah命令生成jni头文件的方法,第一种为大众所知的javah命令,第二种为整合javah到eclipse里面.推荐第二种方式,方便快捷,随时修改随时生成 0:前提和条件: 1:u ...
- PHP isset()与empty()的使用区别详解(转)
通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言.庞大的函数库支持着PHP语言功能的实现.下面我们为大家介绍有关PHP函数isset()与empty()的相关用法. PHP的 ...
- MongoDB学习(2)—Node.js与MongoDB的基本连接示例
前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务,在test数据库中插入一条实例数据: db. ...
- 在Salesforce中处理Email的发送
在Salesforce中可以用自带的 Messaging 的 sendEmail 方法去处理Email的发送 请看如下一段简单代码: public boolean TextFormat {get;se ...
- Java优化之输出十万以内的质数
(1)未经优化时所耗费的时间: public class PrimeNumber { public static void main(String[] args) { long start = Sys ...
- 关于如何在MFC工程中输入不同的数据进行调试
我们可以采用c++的文件输入输出来进行调试 这样就绕过了不能使用黑窗口输入数据就不能调试的思维定式 不是黑窗口的我们都可以考虑用文件流输入输出 或者用控件来输入? http://blog.csdn.n ...
- 时间和地域三级联动选择器(Android-PickerView-master)
先附上下载和效果展示地址 https://github.com/saiwu-bigkoo/Android-PickerView 之后说一下程序依赖后会遇到的问题Error:(2, 0) Plugin ...
- free store VS heap(自由存储区VS堆)
1. free store VS heap free store (自由存储区)和 heap (堆),在C/C++中经常会遇到.他们是否有区别呢? 偶最早发现这两个概念性问题是在<Excepti ...
- .NET运用AJAX 总结及其实例
1.AJAX简介 (1.没有AJAX会怎么样?普通的ASP.Net每次执行服务端方法的时候都要刷新当前页面,比如实现显示服务器的时间.每次都要刷新页面的坏处:页面刷新打断用户操作.速度慢.增加服务器的 ...
- 分享Kali Linux 2016.2第42周镜像文件
分享Kali Linux 2016.2第42周镜像文件Kali Linux官方在10月16日发布Kali Linux 2016.2的第42周镜像文件.这一次不再像上几次,推迟提供32位镜像文件,而是同 ...