题解 | #数组分组#

数组分组

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

import java.util.Scanner;

// 注意类名必须为 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 five = 0;
            int three = 0 ;
            int[] arr = new int [a];
            for (int i = 0 ; i < a ; i++) {
                int b = in.nextInt();
                if (b % 5 == 0 && b % 3 == 0) {
                    arr[i] = b;
                } else if (b % 5 == 0) {
                    five += b;
                } else if (b % 3 == 0) {
                    three += b;
                } else {
                    arr[i] = b;
                }

            }
            

            System.out.println(getRes(five, three, arr, 0));
        }
    }
    public static boolean getRes(int five, int three, int [] arr, int i) {
        if (i == arr.length && five == three) {
            return true;
        } else if (i == arr.length && five != three ) {
            return false
                   ;
        }

        return  getRes(five + arr[i], three, arr, i + 1) ||
                getRes(five, three  + arr[i], arr, i + 1);

    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
粗心的熊熊求求offer:什么内容都没有还弄两页
点赞 评论 收藏
分享
在瑞幸干两年,奥特曼都得闪灯
投递瑞幸咖啡(中国)有限公司等公司7个岗位 >
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务