`
digiter
  • 浏览: 118409 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ICPC编码建议

    博客分类:
  • ICPC
 
阅读更多
写代码最重要的是清晰,包括思路的清晰和代码结构的清晰。我们无法保证写的代码一定是正确的,但我们可以保证自己是在头脑清晰的情况下书写,并且通过不断的练习,用更加清晰的代码结构实现。越清晰,程序就越可能正确,并且即使出错也很容易看到问题。

0)
在能过题的情况下,最朴素最好写的方式就是最好的。

1)
double x = 0;
scanf("%lf", x);     // &x
printf("%lf\n", x);  // output double -> %f
printf("%.6f\n", 0); // undef
// best practice
printf("%.4f\n", x + EPS);
2)
int a[10];
sizeof(a); // 10 * 4
void gao(int a[]) {
	memset(a, 0xff, sizeof(a)); // sizeof(a) == 4
	// best practice
	fill(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 practice
	fill(dist, dist + n, 0x3f3f3f3f);
	...
}

4)
// output %
printf("%%");
// output hex, oct
print("%x%o", a, b);

5)
int a = 3;
if (a = 3) { // ==
}

6)
int x = -7, m = 3;
printf("%d\n", x % m);
// best practice
printf("%d\n", (x % m + m) % m);

7)
long long a = 1 << 40;
// best practice
long long a = 1LL << 40;

8)
long long a;
printf("%I64d\n", a); // under windows
printf("%lld\n", a);  // under linux

9)
int main() {
	int a[10 * 1000 * 1000]; // too large in stack
	...
}
// best practice
int a[10 * 1000 * 1000];
int main() {
	...
}
// or
int main() {
	vector<int> a;
	...
}

10)
char a[11];
gets(a); // unsafe
// best practice
fgets(a, 11, stdin);

11)
double a = 1.0, b = 1.0 / 888888888888888888 * 888888888888888888;
printf("%s\n", a == b ? "same" : "diff");
// best practice
int 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 heap
priority_queue< double, vector<double>, greater<double> > que;
// unique
vector<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/




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics