Tips & Tricks (转)
Tips & Tricks
- Brute force it when you can
- KISS: Simple is smart!
- Hint: focus on limits (specified in problem statement)
- Waste memory when it makes your life easier (if you can get away with it)
- Don't delete your extra debugging output, comment it out
- Optimize progressively, and only as much as needed
- Keep all working versions!
- Code to debug:
-
- whitespace is good,
- use meaningful variable names,
- don't reuse variables,
- stepwise refinement,
- COMMENT BEFORE CODE.
- Avoid pointers if you can
- Avoid dynamic memory like the plague: statically allocate everything.
- Try not to use floating point; if you have to, put tolerances in everywhere (never test equality)
- Comments on comments:
- Not long prose, just brief notes
- Explain high-level functionality: ++i; /* increase the value of i by */ is worse than useless
- Explain code trickery
- Delimit & document functional sections
- As if to someone intelligent who knows the problem, but not the code
- Anything you had to think about
- Anything you looked at even once saying, "now what does that do again?"
- Always comment order of array indices
- Keep a log of your performance in each contest: successes, mistakes, and what you could have done better; use this to rewrite and improve your game plan!
技巧&诀窍
- 穷举,如果能这么做
- 保持简单,傻瓜 (KISS = Keep It Simple, Stupid):简化就是聪明!
- 要点:注意限制条件(题目描述)
- 浪费内存空间吧,当它会让你的生活变得容易时。
- 不要删除你调试的额外输出,注释掉它。
- 不断优化,但是只要满足你的需求就可以了。
- 保留下所有的代码版本!
- 调试代码:
-
- 空格是个好东西
-
- 使用有实际意义的变量名
-
- 不要重复使用变量
-
- 逐步完善
-
- 在代码之前写注释
- 可以的话,尽量避免指针
- 像避免灾难一样避免动态分配内存:静态分配所有变量。
- 试着不要去用浮点数 ;如果不得不用,在所有的地方去容差(不要用 == 判断相等)
- 对注释的评论:
-
- 不要大段散文,只要简单的注释。
-
- 解释高级的功能:++i; /* 把i自增 */ 写出这样的注释比没有任何注释还糟糕
-
- 解释难懂的代码
-
- 分割&功能的模块化
-
- 让聪明的人懂你的程序,而不是代码
-
- 所有的事情你都要去思考
-
- 对所有你第一次看到的东西,说“我怎么把它再做一遍?”
-
- 总是去说明每个数组的意义
- 记录你每一次比赛的表现:优点、错误、哪些地方可以做得更好;用这些来重写一遍,改进你的比赛计划!
转载请注明出处:http://www.cnblogs.com/ygdblogs

浙公网安备 33010602011771号