本文主要是介绍华为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;elsenegative+=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;elsecout<<"false"<<endl;}else{cout<<"false"<<endl;}}else{cout<<"false"<<endl;}}return 0;
}
这篇关于华为OJ——201301 JAVA题目0-1级的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!