CSP认证(2022-06-12)

The more people you love, the weaker you are.
The things we love destroy us every time.

\(vscode\)也配置好了,\(html\)慢慢摸索着也能写些简单的本地网页了,\(CSP\)报名了,等一个小时后的两点\(N2\)也可以报名了。一切都刚刚开始,一切的一起,从结束中。太阳底下没有新鲜事,每次日出都是盛大开场
假面的小丑,上台了,不管他愿不愿意,只是履行着久远岁月最初的约定
尽管只剩他一个人记得约定
尽管步履蹒跚,跌跌撞撞
男人,就是为了一个遥远的约定不惜代价,浪漫至死的笨蛋

It's a trick, a shadow on the wall. And a very small man can cast a very large shadow.

  • 归一化处理
AC代码
# include "bits/stdc++.h"
using namespace std;
int main() {
	int n;
	scanf("%d", &n);
	vector<double> a(n + 1);
	double sum = 0;
	for(int i = 1; i <= n; ++i) scanf("%lf", &a[i]), sum += a[i];
	double ave = sum / (double) n;
	sum = 0;
	for(int i = 1; i <= n; ++i) sum += (a[i] - ave) * (a[i] - ave);
	double dev = sqrt(sum / (double) n);
	for(int i = 1; i <= n; ++i) printf("%.16lf\n", (a[i] - ave) / dev);
	return 0;
}
然而,比$AC$代码更有价值的是
60分(精度问题)代码
# include "bits/stdc++.h"
using namespace std;
double a[1003];
int main() {
freopen("in.txt", "r", stdin);
	int n;
	scanf("%d", &n);
//	vector<long double> a(n + 1); 
	for(int i = 1; i <= n; ++i) {
		scanf("%lf", &a[i]);
	}
//	if(n == 1) {
//		printf("0");
//		return 0;
//	}
	double ave = accumulate(a + 1, a + n + 1, 0) / (double) (1.0 * n);
	double sum = accumulate(a + 1, a + n + 1, 0, [ave](double sum, double val) {
		return sum + (ave - val) * (ave - val);
	});
	
	double dev = sqrt(sum / (double) (1.0 * n));

	auto calc = [&](double x) {
		return (x - ave) / dev;
	};
	for(int i = 1; i <= n; ++i) {
		printf("%.16lf\n", calc(a[i]));
	}
	return 0;
}
同样的思路,简单的过程,却在精度上产生了截然不同的效果。 至少,我认识了$accumulate$的危险,就不会为了炫技而使用它了

image

posted @ 2022-08-25 01:07  邱涵的秘密基地  阅读(111)  评论(0编辑  收藏  举报