题解 | #数组分组#

数组分组

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);

    }
}

全部评论

相关推荐

Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
05-23 19:02
吉林大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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