编程语言
首页 > 编程语言> > 201301 JAVA题目0-1级, 华为笔试题

201301 JAVA题目0-1级, 华为笔试题

作者:互联网


import java.util.*;
public class Main {
    static boolean dfs(List<Integer> list, int sum3, int sum5, int u) {
        if(u == list.size()) {
            return sum3 == sum5;
        } 
        return dfs(list, sum3+list.get(u), sum5, u+1) || dfs(list, sum3, sum5+list.get(u), u+1);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            int[] a = new int[n];
            int sum3 = 0, sum5 = 0;
            List<Integer> list = new ArrayList<>();
            for(int i=0; i <n; i++) {
                a[i] = sc.nextInt();
                if(a[i] % 3 == 0) sum3 += a[i];
                else if(a[i] % 5 == 0) sum5 += a[i];
                else {
                    list.add(a[i]);
                }
            }
            boolean res = dfs(list, sum3, sum5, 0);
            System.out.println(res);
        }
    }
}

标签:JAVA,int,sum3,sum5,list,201301,dfs,华为,new
来源: https://www.cnblogs.com/lixyuan/p/13226575.html