题解 | #数组分组#

数组分组

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

import java.util.*;
import java.util.stream.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
        List<Integer> otherList = new ArrayList<>();
        int fiveSum = 0;
        int threeSum = 0;
        int sum = 0;
        int len = in.nextInt();
        while (in.hasNextInt()) {
            int n = in.nextInt();
            if (n % 5 == 0) {
                fiveSum += n;
            } else if (n % 3 == 0) {
                threeSum += n;
            } else {
                otherList.add(n);
            }
            sum += n;
        }
        if (otherList.size() == 0) {
            if (fiveSum == threeSum) {
                System.out.print(true);
                return;
            } else {
                System.out.print(false);
                return;
            }
        }
        if (sum % 2 != 0) {
            System.out.print(false);
            return;
        }
        if (dfs(otherList, sum / 2 - fiveSum, 0)) {
            System.out.print(true);
        } else {
            System.out.print(false);
        }

        // int dif = fiveSum - threeSum;
        // boolean[] visited = new boolean[otherList.size()];
        // if (dfs(dif, otherList, 0, visited)) {
        //     System.out.print(true);
        // } else {
        //     System.out.print(false);
        // }
    }

    private static boolean dfs(List<Integer> otherList, int target, int start) {
        if (start == otherList.size()) {
            return target == 0;
        }
        if (dfs(otherList, target - otherList.get(start), start + 1)
                || dfs(otherList, target, start + 1)) {
            return true;
        } else {
            return false;
        }
    }

// 24
// -3 -3 0 -2 3 -2 5 -5 -4 -1 0 -4 2 -1 -4 4 4 -1 2 1 5 1 3 -3
// 超时
    // private static boolean dfs(int dif, List<Integer> otherList, int index,
    //                            boolean[] visited) {
    //     visited[index] = true;
    //     boolean visiteAll = true;
    //     for (int i = 0; i < visited.length; i++) {
    //         if (!visited[i]) {
    //             visiteAll = false;
    //             break;
    //         }
    //     }
    //     int temp = otherList.get(index);
    //     if (visiteAll) {
    //         if (dif + temp == 0 || dif - temp == 0) {
    //             return true;
    //         } else {
    //             return false;
    //         }
    //     }
    //     for (int i = 0; i < visited.length; i++) {
    //         if (!visited[i]) {
    //             if (dfs(dif + temp, otherList, i, visited)
    //                     || dfs(dif - temp, otherList, i, visited)) {
    //                 return true;
    //             } else {
    //                 visited[i] = false;
    //             }
    //         }
    //     }
    //     return false;
    // }
}

全部评论

相关推荐

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