首页 > 试题广场 >

数组分组

[编程题]数组分组
  • 热度指数:113289 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入int型数组,询问该数组能否分成两组,使得两组中各元素加起来的和相等,并且,所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),不是5的倍数也不是3的倍数能放在任意一组,可以将数组分为空数组,能满足以上条件,输出true;不满足时输出false。

数据范围:每个数组大小满足 ,输入的数据大小满足

输入描述:

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



输出描述:

返回true或者false

示例1

输入

4
1 5 -5 1

输出

true

说明

第一组:5 -5 1
第二组:1      
示例2

输入

3
3 5 8

输出

false

说明

由于3和5不能放在同一组,所以不存在一种分法。      
import java.util.ArrayList;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        ArrayList ao = new ArrayList();
        int sum5 = 0;
        int sum3 = 0;
        int sumo = 0;
        while(in.hasNextInt()){
            int a = in.nextInt();
            if(a%5 == 0){
                sum5 += a;
            } else if(a%3 == 0){
                sum3 += a;
            } else {
                ao.add(a);
                sumo += a;
            }
        }
        if((sum5 + sum3 + sumo)%2 != 0){
            System.out.print(false);
        } else {
            int sum = (sum3 + sum5 + sumo)/2 -sum5;
            if(cansum(ao, ao.size() - 1, sum)){
                System.out.print(true);
            } else {
                System.out.print(false);
            }
        }
    }
    private static boolean cansum(ArrayList a, int i, int sum){
        if(a.size() == 0 || i < 0){
            return sum == 0;
        }
        return cansum(a, i - 1 , sum) || cansum(a, i - 1, sum - a.get(i));
    }
}
发表于 2024-05-23 10:18:03 回复(0)
普通递归就好


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


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int total= in.nextInt();

        List<Integer> arr5 = new ArrayList(total);
        List<Integer> arr3 = new ArrayList(total);
        otherArr = new ArrayList(total);
        for (int i = 0; i < total; i++) {
            int item = in.nextInt();
            if (0 == item % 5) {
                arr5.add(item);
            } else if (0 == item % 3) {
                arr3.add(item);
            } else {
                otherArr.add(item);
            }
        }

        absOf5And3 = Math.abs(arr5.stream().mapToInt(i -> i).sum() -
                arr3.stream().mapToInt(i -> i).sum());

        otherArrSize = otherArr.size();

        if (0 == otherArrSize) {
            if (0 == absOf5And3) {
                System.out.println("true");
                return;
            } else {
                System.out.println("false");
                return;
            }
        }

        signArr = new int[otherArrSize];
        IntStream.range(0, otherArrSize).forEach(i -> signArr[i] = -1);

        sumAndCheckEqual();
        
        calc(otherArrSize - 1);
    }

    private static int absOf5And3;
    
    private static List<Integer> otherArr;
    private static int otherArrSize;

    private static int[] signArr;
    
    private static void sumAndCheckEqual() {
       if (absOf5And3 == Math.abs(IntStream.range(0, otherArrSize).map(i -> otherArr.get(i) * signArr[i]).sum())) {
            System.out.println("true");
            System.exit(0);
        }
    }

    private static void calc(int plusSignSwitchIndex) {
        if (0 > plusSignSwitchIndex) {
            System.out.println("false");
            System.exit(0);
        }

        if (-1 == signArr[plusSignSwitchIndex]) {
            signArr[plusSignSwitchIndex] = 1;
            for (int i = 1 + plusSignSwitchIndex; i < otherArrSize; i++) {
                signArr[i] = -1;
            }            
            sumAndCheckEqual();
            
            calc(otherArrSize - 1);
        } else {
            calc(plusSignSwitchIndex - 1);
        }
    }
}


编辑于 2023-12-07 15:56:27 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static Stack<Integer> stack = new Stack();
    public static boolean flag = false;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        int[] a = new int[n];
        int sum3 = 0;
        int sum5 = 0;
        for(int i = 0; i < n; i++){
            a[i] = in.nextInt();
            if(a[i]%3 == 0){
                sum3 += a[i];
            }else if(a[i] % 5 == 0){
                sum5 += a[i];
            }else{
                stack.push(a[i]);
            }
        }
        dfs(sum3, sum5);
        System.out.println(flag);
    }

    public static void dfs(int sum3, int sum5) {
        if (stack.empty() && sum3 == sum5) {
            flag = true;
        }
        if (!stack.empty()) {
            int n = stack.pop();
            dfs(sum3+n, sum5);
            dfs(sum3, sum5+n);
            stack.push(n);
        }
    }
}

发表于 2023-11-28 17:45:46 回复(0)
// 用的递归,很方便,不知道正确性,欢迎大家指正
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = in.nextInt();
            }

            boolean result = canPartition(nums, 0, 0, 0);
            System.out.println(result ? "true" : "false");
        }

        in.close();
    }

    // 递归的方式解决,index表示当前的数字在数组中的位置,sum1表示第一组数的和,sum2表示第二组数的和
    private static boolean canPartition(int[] nums, int index, int sum1, int sum2) {
        // 递归的终止条件,当遍历到最后一位时,检查sum1是否等于sum2,如相等,则返回true
        if (index == nums.length) {
            return Math.abs(sum1 - sum2) == 0;
        }
        // 将5放进第一组数,sum1加上该数,index + 1表示遍历到下一组数
        if (nums[index] % 5 == 0) {
            return canPartition(nums, index + 1, sum1 + nums[index], sum2);
        } else if (nums[index] % 3 == 0) {  //将3放进第二组数
            return canPartition(nums, index + 1, sum1, sum2 + nums[index]);
        } else {
            // 尝试把非3非5的数放进第一组
            if (canPartition(nums, index + 1, sum1 + nums[index], sum2)) {
                return true;
            }

            // 尝试把非3非5的数放进第二组
            if (canPartition(nums, index + 1, sum1, sum2 + nums[index])) {
                return true;
            }
        }
        // 所有的方法都没法分,返回false
        return false;
    }
}

发表于 2023-07-23 03:10:26 回复(0)
import java.io.*;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            int n = Integer.parseInt(str);
            String[] strArr = br.readLine().split(" ");
            ArrayList<Integer> list = new ArrayList<>();
            int sum3=0,sum5=0,sum=0;
            for (String s : strArr){
                int num=Integer.parseInt(s);
                // 3的倍数则加到sum3
                if(num%3==0) sum3 += num;
                // 5的倍数则加到sum5
                else if(num%5==0) sum5 += num;
                // 不是3、5的倍数则加到集合中
                else list.add(num);
                // 所有数之和
                sum+=num;
            }
            // 两个相等的数之和为偶数,如果sum不为偶数,说明两个数组的和不相等
            if(sum%2!=0) System.out.println(false);
            else{
                // target代表能与sum3相加得到sum/2的值,即判断list是否存在和或值为target的元素,
                // 即用list的元素凑出target
                // 假设存在满足条件的元素,则得:sum=sum3+sum5+c(list的元素和)和sum/2=sum3+a
                // 由此可得sum/2=sum5+(c-a),即若能凑出a使得sum/2=sum3+a,则list剩余元素和+sum5
                // 必等于sum/2,所以target可以为sum/2-sum3或者sum/2-sum5
                int target=sum/2-sum3;
                System.out.println(solution(list,target,0));
            } 
        }
    }
    private static boolean solution(List<Integer>list, int target, int index) {
        // 递归终止条件
        if (index == list.size()) return target==0;
        // 核心在target-list.get(index),在solution(list,target-list.get(index),index+1)中
        // 由于index+1,所以它会依次减去list中的元素,直到index == list.size()(即减完最后一个元素),
        // 而solution(list,target,index+1)则表示跳过第index个元素进入递归,即target不减第index个元素,
        // 所以每次递归都会出现减或者不减第index个元素的情况,因此递归终止时,完成了对list中元素所有可能类型
        // 的拼凑。当index == list.size()递归终止时,如果 target==0,说明能在list元素中凑出sum/2-sum3。
        // 可对list={1,2,5,7}进行纸上递归计算演示
        return solution(list,target-list.get(index),index+1)||solution(list,target,index+1);
    }
}

发表于 2023-07-12 16:33:40 回复(0)
看了一圈,基本都要暴力搜索,动态规划(大约是?)只有n2复杂度。
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        ArrayList<Integer> list = new ArrayList<>();//排除3、5倍数后剩下的数
        HashSet<Integer> differs = new HashSet<>();//能构造出的差值的集合
        int d = 0;
        for (int i = 0; i < n; i++) {//算出3、5数组分配完后之间的和的差值d
            int a = in.nextInt();
            if (a % 5 == 0) {
                d += a;
            }
            else if (a % 3 == 0) {
                d -= a;
            }
            else {
                list.add(a);
            }
        }
        d = Math.abs(d);//接下来我们要判断是否可以借助余下的数构造出d这个差值
        differs.add(0);//余下的数一个都没用的时候构造出的差值为0
        for (int x : list) {//计算出当多使用了list中的一个数x的时候,能构造出的差值集合
            HashSet<Integer> toAdd = new HashSet<>();
            for (int y : differs) {//新的能构造出的差值集合是原先能构造出的差值加减新使用的数x,符号对应x放置于哪边的数组
                toAdd.add(Math.abs(x - y));//同样,符号改变只让放置方式镜像,这里简便处理为绝对值
                toAdd.add(Math.abs(x + y));
            }
            differs = toAdd;
        }
        System.out.print(differs.contains(d));//看看差值集合里面能不能找到d,找到了就宣布构造成功。
    }
}

发表于 2023-07-01 00:45:06 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        LinkedList<Integer> list = new LinkedList<>();
        int sum5 = 0;
        int sum3 = 0;
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            int num = in.nextInt();
            if (num % 5 == 0) {
                sum5 += num;
            } else if (num % 3 == 0) {
                sum3 += num;
            } else {
                list.add(num);
            }
        }
        int cha = Math.abs(sum3 - sum5);
        System.out.print(isDivide(cha, list));
    }
    public static boolean isDivide(int cha, LinkedList<Integer> scList) {
        LinkedList<Integer> list = new LinkedList<>(scList);
        if (list.size() == 0) {
            return cha == 0;
        } else {
            int listFirst = list.poll();
            int newCha1 = Math.abs(listFirst + cha);
            int newCha2 = Math.abs(listFirst - cha);
            return isDivide(newCha1, list) || isDivide(newCha2, list);
        }
    }
}
发表于 2023-04-15 13:04:26 回复(0)
//将一个数组的所有元素都变成正负数,将所有元素相加放到result里面
    public static List<Integer> getAllNumCount(List<Integer> param,int index,List<Integer> result){
        //递归结束标志
        if(index ==param.size()){
            return result;
        }
        int indexV = param.get(index);
        List<Integer> newResult = new ArrayList<Integer>();
        for(int i=0;i<result.size();i++){
            int value = result.get(i);
            newResult.add(value+indexV);
            newResult.add(value-indexV);
        }
        //避免因为结果地址经常变动导致失败,所以不用newResult返回
        result.clear();
        result.addAll(newResult);
        getAllNumCount(param,index+1,result);//当index <param.size(),递归调用当前方法体,index+1
        return result;//这里要返回个结果,否则会报错,因为当index <param.size()需要这个返回
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int nn = in.nextInt();
            List<Integer> list1 = new ArrayList<Integer>();
            List<Integer> list2 = new ArrayList<Integer>();
            List<Integer> list3 = new ArrayList<Integer>();
            for(int i=0;i<nn;i++){
                int b = in.nextInt();
                if(b%3==0){
                    list1.add(b);
                }else if(b%5==0){
                    list2.add(b);
                }else{
                   list3.add(b);
                }              
            }
            //计算3倍数,5倍数的差值
            int num1 = 0;
            for(int i=0;i<list1.size();i++){
                num1+=list1.get(i);
            }
            int num2 = 0;
            for(int i=0;i<list2.size();i++){
                num2+=list2.get(i);
            }
            int abs = Math.abs(num1-num2);//3倍数,5倍数的差值的绝对值
            //list3数组的数据都变成带+-号的值相加,然后所有的可能性是否是+-abs
            if(list3.size()==0){
                if(num1==num2){
                     System.out.println(true);
                }else{
                     System.out.println(false);
                }
            }else{
                List<Integer> resultParam = new ArrayList<Integer>();
                resultParam.add(0);
                //计算出来list3的所有可能值,跟abs挨个比对
                List<Integer> result = getAllNumCount(list3,0,resultParam);
                boolean flag =false;
                for(int i=0;i<result.size();i++){
                    int value = result.get(i);
                    if(Math.abs(value)==abs){
                        flag =true;
                        break;
                    }
                }
                if(flag){
                    System.out.println(true);
                }else{
                    System.out.println(false);
                }
            }
        }
    }
发表于 2023-04-14 00:55:55 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), part = 0, low = 0, sum = 0;
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = in.nextInt();
        }
        ArrayList<Integer> nums = new ArrayList<>();
        for(int num : arr){
            if(num % 5 != 0 && num % 3 != 0){
                nums.add(num);
            }
            if(num % 5 == 0){
                part += num;
            }
            if(num < 0){
                low += num;
            }
            sum += num;
        }
        if(sum % 2 != 0){
            System.out.println(false);
            return;
        }
        // 因为有负数,选择使用map
        HashMap<Integer, Boolean> dp = new HashMap<>();
        dp.put(0, true);
        for(int num : nums){
            dp.put(num, true);
        }
        Collections.sort(nums);
        // 使用类似0/1背包问题类似的思路
        for(int i = 0; i < nums.size(); i++){
            for(int j = sum / 2 - part; j >= low; j--){
                if(!dp.getOrDefault(j, false)){
                    dp.put(j, dp.getOrDefault(j - nums.get(i), false));
                }
            }
        }
        if(dp.getOrDefault(sum / 2 - part, false))
            System.out.println(true);
        else
            System.out.println(false);
    }
}

发表于 2023-04-09 14:42:43 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num=in.nextInt();
        int sum3=0;
        int sum5=0;
        ArrayList<Integer> listO=new ArrayList<>();
        for(int i=0;i<num;i++){
            int temp=in.nextInt();
            if(temp%3==0){
                sum3+=temp;//含3 
            }else if(temp%5==0){
                sum5+=temp;//含5
            }else{
                listO.add(temp);// 不含的先不加
            }
        }
        int len=1;
        for(int i=1;i<=listO.size();i++){//每个中立元素都有2种可能
            len*=2;
        }
        for(int i=0;i<len;i++){//遍历每个元素的每种可能
            int sum3Temp=sum3;
            int sum5Temp=sum5;
            int iTemp=i;
            for(int j=0;j<listO.size();j++){//用%2来决定加入哪个数组
                if( iTemp%2==1){
                    sum3Temp+=listO.get(j);
                }else{
                    sum5Temp+=listO.get(j);
                }
                iTemp/=2;
            }
            if(sum3Temp==sum5Temp){
                System.out.print(true);
                return;
            }
        }
        System.out.print(false);
    }
}

发表于 2022-12-26 11:30:59 回复(0)
递归比较通俗易懂,但是加了记忆化搜索就报错,这是为何
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n];
        int sum = 0;
        for (int i = 0; i < n; i++) {
            nums[i] = in.nextInt();
            sum += nums[i];
            if (nums[i] % 3 == 0) {
//                3的倍数,直接舍弃
                nums[i] = 0;
            }
        }
        if (sum % 2 != 0) {
//            和不是偶数,直接返回
            System.out.println(false);
            return;
        }
        sum /= 2;
//        使用nums数组能否组成 sum
        System.out.println(dp(nums, 0, sum));
    }

    /**
     * dp[startIndex:]能否拼凑出target
     */
    private static boolean dp(int[] nums, int startIndex, int target) {
        if (startIndex == nums.length) {
            return target == 0;
        }
        if (nums[startIndex] % 5 == 0) {
//            5的倍数,必须放在当前组
            return dp(nums, startIndex + 1, target - nums[startIndex]);
        } else {
//            不是5的倍数,放在哪一组都可以
            return dp(nums, startIndex + 1, target) ||
                   dp(nums, startIndex + 1, target - nums[startIndex]);
        }
    }
}


发表于 2022-11-11 16:55:38 回复(0)
非常好理解的算法
利用回溯算法
sum = mod5(5的整倍数的和) + mod3(3的整倍数的和) + other(其他数的和)
target = sum/2
整理出来这些变量之后,接下来只要遍历other 在other列表里找到一个组合 使得
other + mod5 == target
或者
other + mod3 == target 即可
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        
        int sum = 0;
        int mod3 = 0;
        int mod5 = 0;
        ArrayList<Integer> other = new ArrayList<>();
        while (in.hasNextInt()) 
        { // 注意 while 处理多个 case
            n = in.nextInt();
            if(n % 5 == 0)
            {
                mod5 += n;
            }
            else if(n % 3 == 0)
            {
                mod3 += n;
            }
            else
            {
                other.add(n);
            }
            sum += n;
        }
        
        if(sum%2 != 0)
        {
            System.out.println(false);
        }
        else
        {
            System.out.println(trav(mod3, mod5, sum/2, other, new boolean[other.size()], 0, 0));
        }
    }

    private static boolean trav(int mod3, int mod5, int target, ArrayList<Integer> nums, boolean[] used, int idx, int sum)
    {
        if(sum + mod3 == target) return true;
        if(sum + mod5 == target) return true;

        for(int i = idx; i < nums.size(); i++)
        {
            if(used[i] == false)
            {
                used[i] = true;
                if(trav(mod3, mod5, target, nums, used, i + 1, sum+nums.get(i)))
                {
                    return true;
                }
                used[i] = false;
            }
        }
        return false;
    }
}


发表于 2022-10-20 04:46:34 回复(0)
递归
/**
 * @author YXQ
 * @create 2022/8/23  15:07
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * HJ93 数组分组
 * 输入int型数组,询问该数组能否分成两组,使得两组中各元素加起来的和相等,并且,所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),不是5的倍数也不是3的倍数能放在任意一组,可以将数组分为空数组,能满足以上条件,输出true;不满足时输出false。
 *
 * 输入描述:
 * 第一行是数据个数,第二行是输入的数据
 * 输出描述:
 * 返回true或者false
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] nums=new int[n];
        for (int i=0;i<n;i++){
            nums[i]=sc.nextInt();
        }
        if(dfs(nums, 0,0,0)) {
            System.out.println("true");
        }
        else System.out.println("false");

    }

     /**
     * 递归函数3
     * 递归体:将nums[index]这个数 给分组1或者分组2
     * @param nums
     * @param index
     * @param res1
     * @param res2
     * @return
     */
    public static boolean dfs(int[] nums, int index,int res1,int res2){
        if(index==nums.length){
            if (res1==res2){
                return true;
            }else return false;
        }
        boolean b1=false;
        boolean b2=false;
//        选第一组的数
//        把nums[index]这个数给分组1
        if(!checkArr2(nums[index])) b1=dfs(nums,index+1,res1+nums[index],res2);
//        选第二组的数
//        把nums[index]这个数给分组2
        if(!checkArr1(nums[index]))b2=dfs(nums,index+1,res1,res2+nums[index]);
        return b1||b2;

    }
    
    public static boolean checkArr1(int num){
        if(num%5==0)return true;
        else return false;
    }
    public static boolean checkArr2(int num){
        if(num%3==0&&num%5!=0)return true;
        else return false;
    }

}


发表于 2022-08-23 17:45:26 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: MiaoYangYang
 * @Email: miaoyang@mail.ustc.edu.cn
 * @Create: 2022-08-14 11:02:14
 **/
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] s = br.readLine().split(" ");
        List<Integer> not3AndNot5 = new ArrayList<>();
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int num = Integer.parseInt(s[i]);
            if (num % 5 == 0) {
                sum += num;
            } else if (num % 3 == 0) {
                sum -= num;
            } else {
                not3AndNot5.add(num);
            }
        }
        System.out.println(dfs(not3AndNot5, sum, 0));
    }

    public static boolean dfs(List<Integer> not3AndNot5, int target, int i) {
        if (i == not3AndNot5.size()) {
            return target == 0;
        }
        return dfs(not3AndNot5, target + not3AndNot5.get(i), i + 1) || dfs(not3AndNot5, target - not3AndNot5.get(i), i + 1);
    }
}

发表于 2022-08-14 11:04:51 回复(0)
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> a = new ArrayList<>(n);
        List<Integer> b = new ArrayList<>(n);
        List<Integer> c = new ArrayList<>(n);
        long sa = 0l;
        long sum = 0l;
        for (int i = 0; i < n; i++) {
            int j = sc.nextInt();
            if (j % 5 == 0) {
                a.add(j);
                sa += j;
            } else if (j % 3 == 0) {
                b.add(j);
                sum += j;
            } else {
                c.add(j);
                sum += j;
            }
        }
        sum += sa;
        if (Math.abs(sum % 2) == 1) {
            System.out.println(false);
            return;
        }
        long d = sum / 2 - sa;
        if (dfs(c, d, 0)) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }
    }

    static boolean dfs(List<Integer> a, long m, int i) {
        if (i == a.size()) {
            return m == 0;
        }
        if (dfs(a, m, i + 1)) {
            return true;
        }
        if (dfs(a, m - a.get(i), i + 1)) {
            return true;
        }
        return false;
    }


}

发表于 2022-07-06 01:00:47 回复(0)
直接判断和是否能被2整除,能整除返回true,不能返回false。 能跑通23/26的用例
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            while (sc.hasNextInt()) { // 注意 while 处理多个 case
                
            int n = sc.nextInt();
            int[] chushi = new  int[n];
            int sum =0;
            for(int m=0;m<n;m++){
                sum+=sc.nextInt();
            }
                
           if(sum%2==0){
               System.out.println("true");
           }else{
               System.out.println("false");
           }
          
            }
    }
}
发表于 2022-06-13 19:36:35 回复(1)