华为OJ——201301 JAVA题目0-1级

201301 JAVA题目0-1

题目描述

编写一个函数,传入一个int型数组,返回该数组能否分成两组,使得两组中各元素加起来的和相等,并且,所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),能满足以上条件,返回true;不满足时返回false 

输入描述:

第一行是数据个数,第二行是输入的数据

输出描述:

返回true或者false

输入例子:

4

1 5 -5 1

输出例子:

true

解答代码:

#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<algorithm>
#include<sstream>
#include<cmath>
using namespace std;

int main()
{ 
    int data;
	int i,j,n;
	while(cin >> n)
	{
		int sum=0 , sum_5=0 , sum_3=0 , sum_other=0 , positive=0 , negative=0 , temp=0 , dis=0;
		for(i=0;i<n;i++)
		{
			cin >> data;
			sum+=data;
			if(data % 5 == 0)
				sum_5 += data;
			else if(data % 3 == 0)
				sum_3 += data;
			else
			{
				if(data > 0)
					positive+=data;
				else
					negative+=data;
				sum_other+=data;
			}
		}
		dis=abs(sum_5 - sum_3);
		if((sum_other-dis) %2 ==0)
		{
			temp=sum/2;
			if(temp==(sum_3 + sum_other - ( temp - sum_5 )))
			{
				if(temp-sum_5>=0 && positive>=temp-sum_5 || temp-sum_5<0 &&  negative<= temp-sum_5)
					cout<<"true"<<endl;
				else
					cout<<"false"<<endl;
			}
			else
			{
				cout<<"false"<<endl;
			}
		}
		else
		{
			cout<<"false"<<endl;
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值