A,80%的代码,WA麻了,求助!!!

#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,m;
map<int,int>mp;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		mp.clear();
		cin>>n;
		int sum=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			sum+=x;
			mp[x]++;
		}
		if(sum%3==0)
		{
			string s="";
			for(auto [x,y]:mp)
			{
				for(int j=0;j<y;j++) s+=to_string(x);
			}
			reverse(s.begin(),s.end());
			if(s[0]!='0'&&s.size()!=0) cout<<s<<endl;
            else if(s[0]=='0'&&s.size()!=0) cout<<0<<endl;
			else cout<<-1<<endl;
		}
		if(sum%3==1)//要删掉1
		{
			if(mp.count(1)) mp[1]--;
			else if(mp.count(4)) mp[4]--;
			else if(mp.count(7)) mp[7]--;
			else if(mp[2]>=2) mp[2]-=2;
			else if(mp[2]>=1&&mp[5]>=1) mp[2]--,mp[5]--;
			else
			{
				cout<<-1<<endl;
				continue;
			}
			
			string s="";
			for(auto [x,y]:mp)
			{
				for(int j=0;j<y;j++) s+=to_string(x);
			}
			reverse(s.begin(),s.end());
			if(s[0]!='0'&&s.size()!=0) cout<<s<<endl;
            else if(s[0]=='0'&&s.size()!=0) cout<<0<<endl;
			else cout<<-1<<endl;
		}
		if(sum%3==2)
		{
			if(mp.count(2)) mp[2]--;
			else if(mp.count(5)) mp[5]--;
			else if(mp.count(8)) mp[8]--;
			else if(mp[1]>=2) mp[1]-=2;
			else if(mp[1]>=1&&mp[4]>=1) mp[1]--,mp[4]--;
			else if(mp[4]>=2) mp[4]-=2;
			else if(mp[1]>=1&&mp[7]>=1) mp[1]--,mp[7]--;
			else
			{
				cout<<-1<<endl;
				continue;
			}
			string s="";
			for(auto [x,y]:mp)
			{
				for(int j=0;j<y;j++) s+=to_string(x);
			}
			reverse(s.begin(),s.end());
			if(s[0]!='0'&&s.size()!=0) cout<<s<<endl;
            else if(s[0]=='0'&&s.size()!=0) cout<<0<<endl;
			else cout<<-1<<endl;
		}
	}
	return 0;
}

}

全部评论
``` 1 7 6 0 8 9 5 3 9  ``` 您的输出:-1 答案:99630
点赞 回复 分享
发布于 2023-11-18 16:57 湖南
分类讨论情况少了,删两个数的情况还有几种没列出来,并且顺序要按照出现的最大值递增顺序(比如删5,5比删2,8要好),我下面这样分类讨论可以过: if(sum%3==1)//要删掉1 { if(mp.count(1)) mp[1]--; else if(mp.count(4)) mp[4]--; else if(mp.count(7)) mp[7]--; else if(mp[2]>=2) mp[2]-=2; else if(mp[2]>=1&&mp[5]>=1) mp[2]--,mp[5]--;             else if(mp[5]>=2) mp[5]-=2;             else if(mp[2]>=1&&mp[8]>=1) mp[2]--,mp[8]--;             else if(mp[5]>=1&&mp[8]>=1) mp[5]--,mp[8]--;             else if(mp[8]>=2) mp[8]-=2; } if(sum%3==2) { if(mp.count(2)) mp[2]--; else if(mp.count(5)) mp[5]--; else if(mp.count(8)) mp[8]--; else if(mp[1]>=2) mp[1]-=2; else if(mp[1]>=1&&mp[4]>=1) mp[1]--,mp[4]--; else if(mp[4]>=2) mp[4]-=2; else if(mp[1]>=1&&mp[7]>=1) mp[1]--,mp[7]--;             else if(mp[4]>=1&&mp[7]>=1) mp[4]--,mp[7]--;             else if(mp[7]>=2) mp[7]-=2; else { cout<<-1<<endl; continue; }
点赞 回复 分享
发布于 2023-11-18 16:30 天津

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务