ICPC编码建议
写代码最重要的是清晰,包括思路的清晰和代码结构的清晰。我们无法保证写的代码一定是正确的,但我们可以保证自己是在头脑清晰的情况下书写,并且通过不断的练习,用更加清晰的代码结构实现。越清晰,程序就越可能正确,并且即使出错也很容易看到问题。
0)在能过题的情况下,最朴素最好写的方式就是最好的。1)double x = 0;scanf("%lf", x); // &xprintf("%lf\n", x); // output double -> %fprintf("%.6f\n", 0); // undef// best practiceprintf("%.4f\n", x + EPS);2)int a[10];sizeof(a); // 10 * 4void gao(int a[]) {memset(a, 0xff, sizeof(a)); // sizeof(a) == 4// best practicefill(a, a + 10, -1);}3)const int inf = 0x7fffffff;void dijkstra(int dist[], int n) {fill(dist, dist + n, inf); // when a + b will get overflow// best practicefill(dist, dist + n, 0x3f3f3f3f);...}4)// output %printf("%%");// output hex, octprint("%x%o", a, b);5)int a = 3;if (a = 3) { // ==}6)int x = -7, m = 3;printf("%d\n", x % m);// best practiceprintf("%d\n", (x % m + m) % m);7)long long a = 1 << 40;// best practicelong long a = 1LL << 40;8)long long a;printf("%I64d\n", a); // under windowsprintf("%lld\n", a); // under linux9)int main() {int a[10 * 1000 * 1000]; // too large in stack...}// best practiceint a[10 * 1000 * 1000];int main() {...}// orint main() {vector<int> a;...}10)char a[11];gets(a); // unsafe// best practicefgets(a, 11, stdin);11)double a = 1.0, b = 1.0 / 888888888888888888 * 888888888888888888;printf("%s\n", a == b ? "same" : "diff");// best practiceint sgn(double x, double eps = 1.0e-9) {return (x > eps) - (x < -eps);}printf("%s\n", sgn(a - b) == 0 ? "same" : "diff");12)// round up(int)(a + EPS)(int)(a + EPS + 0.5)13)// small heappriority_queue< double, vector<double>, greater<double> > que;// uniquevector<int> vec;sort(vec.begin(), vec.end());vec.resize(unique(vec.begin(), vec.end()) – vec.begin());14)总是用cerr输出调试信息,这样在注释调试信息时,搜索cerr就可以很容易注释掉全部的调试信息。15) CodeBlocks在运行窗口粘贴数据的方法16)总是使用fill_n函数http://www.cplusplus.com/reference/algorithm/fill_n/