#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类提供了硬件级别的原子操 ...
随机推荐
- PAT A 1016. Phone Bills (25)【模拟】
题目:https://www.patest.cn/contests/pat-a-practise/1016 思路:用结构体存储,按照名字和日期排序,然后先判断是否有效,然后输出,时间加减直接暴力即可 ...
- Dwz下拉菜单的二级联动
在DWZ文档中对组合框combox的是这样描述的: 在传统的select 用class 定义:class=”combox”, html 扩展:保留原有属性name, 增加了属性:ref. ref 属 ...
- Asp.Net MVC中Controller与View之间传递的Model
Controller --> View 的Model 与 提交表单后 View --> Controller 的Model 不是相同的对象,即:这两个Model为不同的指针,指向不同的地址 ...
- tree view
<TreeView x:Name="treeParameter" Width=" Margin="11,6,11,6" ItemsSource= ...
- 学习linux内核时常碰到的汇编指令(2)
转载:http://blog.sina.com.cn/s/blog_4be6adec01007xvh.html JNGE∶指令助记符——(有符号数比较)不大于且不等于转移(等价于JL).当SF和OF异 ...
- C# 枚举(enum)
public enum EnumStatus { On=0, Off=1 } //获取值:0 Convert.ToInt32(EnumStatus.On); //获取On EnumStatus.On; ...
- DSP using MATLAB 示例Example2.4
n = [0:10]; x = stepseq(0,0,10) - stepseq(10,0,10); [xe,xo,m] = evenodd(x,n); set(gcf,'Color',[1,1,1 ...
- Eclipse 的 Debug 介绍与技巧【转载】
没有任何程序员能够一气呵成的写出没有任何 Bug 的代码,所以很多程序员有相当一部分时间是花费在 Debug 上的,程序调试是每个程序员必须面对的工作.如何使用 Eclipse 进行有效的.尤其是高效 ...
- Tactical Multiple Defense System 二分图
This problem is about a war game between two countries. To protect a base, your country built a defe ...
- linux 服务初识
1. daemon 和 service 系统为了实现某些功能,必须提供一些服务(service),但是service的提供总是需要进程的运行,实现service 的程序我们称为daemon(“守护神” ...