题解 | #数组分组#

数组分组

http://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86

import java.util.ArrayList;
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner in= new Scanner(System.in);
		Solution solution = new Solution();
		//Insert Your Code Here.
		int n=in.nextInt();
		ArrayList<Integer> list=new ArrayList<>();
		int sum1=0,sum2=0;
		for(int i=0;i<n;i++){
			int temp=in.nextInt();
			if(temp%5==0) 
				sum1+=temp;
			else if(temp%3==0) 
				sum2+=temp;
			else 
				list.add(temp);
		}
		in.close();
		System.out.println(solution.core(sum1,sum2,list,0));
	}
}
class Solution {
  //用栈模拟,加入5这个数组或者加入3这个数组
	public boolean core(int sum1,int sum2,ArrayList<Integer> list,int index) {
		if(index==list.size()){
			if(sum1==sum2)
				return true;
			else 
				return false;
		}
		int temp=list.get(index);
		return core(sum1+temp,sum2,list,index+1)||core(sum1,sum2+temp,list,index+1);
	}
}


全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务