跳到正文

个人记录

抽象的函数返回值

Falling_Finch约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前

这是我CSP-S T3的代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
ll n,a[N],ans;
ll p[N];
vector<ll> Red,Blue;
ll work(){
	ll sum=0;
	Red.clear();
	Blue.clear();
	for(int i=1;i<=n;i++)
		if(p[i]) Red.push_back(i);
		else Blue.push_back(i);
	for(int i=1;i<Red.size();i++)
		if(a[Red[i]]==a[Red[i-1]])
			sum+=a[Red[i]];
	for(int i=1;i<Blue.size();i++)
		if(a[Blue[i]]==a[Blue[i-1]])
			sum+=a[Blue[i]];
	ans=max(ans,sum);
}
void dfs(ll x){
	if(x==n+1){
		work();
		return;
	}
	p[x]=0;
	dfs(x+1);
	p[x]=1;
	dfs(x+1);
}
int main() {
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int T;cin>>T;
	while(T--){
		cin>>n;
		ans=0;
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		dfs(1);
		cout<<ans<<endl;
	}
	return 0;
}

这个是0分的。

为什么呢?

因为这一行

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+9;
ll n,a[N],ans;
ll p[N];
vector<ll> Red,Blue;
ll work(){  //这里应当是void
	ll sum=0;
	Red.clear();
	Blue.clear();
	for(int i=1;i<=n;i++)
		if(p[i]) Red.push_back(i);
		else Blue.push_back(i);
	for(int i=1;i<Red.size();i++)
		if(a[Red[i]]==a[Red[i-1]])
			sum+=a[Red[i]];
	for(int i=1;i<Blue.size();i++)
		if(a[Blue[i]]==a[Blue[i-1]])
			sum+=a[Blue[i]];
	ans=max(ans,sum);
}
void dfs(ll x){
	if(x==n+1){
		work();
		return;
	}
	p[x]=0;
	dfs(x+1);
	p[x]=1;
	dfs(x+1);
}
int main() {
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int T;cin>>T;
	while(T--){
		cin>>n;
		ans=0;
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		dfs(1);
		cout<<ans<<endl;
	}
	return 0;
}

把返回值是 voidvoid 的写成 llll ,但是没有返回值,在DEVC++中不会出问题,但是在除了DEVC++以外的所有测评机似乎都会RE……

所以CSP-S挂了20分。。。

警钟长鸣

更新:这个导致了我总分150,(差点就蓝勾了)(差点就差5分一等了(去年135就是差5分一等))

原文