#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类提供了硬件级别的原子操 ...
随机推荐
- [Eclipse][SVN] 在eclipse上安装SVN
以前装过好多次SVN,始终没有一次把安装过程记录下来,这次新装机器,安装SVN插件时一波三折,记录下来免得以后又忘记了. 方法一: 1. 直接通过后台添加URL通过互联网进行安装,直接上图: 2. ...
- Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...
- windows下R语言在终端的运行
在windows下可以有多种方式来运行R,R导论的这些章节给出一些详细的指导. 通常在环境变量离包含R的安装目录类似于R\R-3.1.2\bin\x64的情况下,就可以在CMD下运行R程序了 注意我这 ...
- JavaScript案例一:Window弹窗案例
注:火狐可运行,谷歌不可运行(安全级别高) <!DOCTYPE html> <html> <head> <title>JavaScript 弹窗案例&l ...
- 网上下载的CHM帮助文件打不开的解决办法。
我的机器 装的是 Windows server 2008 操作系统.他的安全性比较高. 我在网上下载了一个 CHM 帮助文档.结果打不开. 现象: 打开时 ,提示 安全警告, 提示:来自Interne ...
- JAVA 加减乘除
package homework; import javax.swing.JOptionPane; public class suanshu { public static void main(Str ...
- css3 -- 渐变
1.Firefox中的线性渐变 E{ background-image:-moz-linear-gradient(point or angle , form-stop, color-stop, to- ...
- 服务器控件和 viewstate
//不会产生处理回发事件的方法.类似客户端html Repeater rep = new Repeater(); DataList dtl = new DataList(); FileUpload f ...
- hadoop 分布式缓存
Hadoop 分布式缓存实现目的是在所有的MapReduce调用一个统一的配置文件,首先将缓存文件放置在HDFS中,然后程序在执行的过程中会可以通过设定将文件下载到本地具体设定如下: public s ...
- iOS 关于iOS开发中的delegate
有A.B两个对象,A要完成某件事,想让B帮它做. 这时候,A中就要实例化一个B的对象b,A还要在头文件中声明协议,然后在B中实现协议中对应的方法. 这时候再把A的delegate设置为b,在需要的地方 ...