暴力枚举

24点

http://www.nowcoder.com/questionTerminal/263fa05acac5424a91214694a1c1eb8f

import java.util.Scanner;

public class Main {

public static int count;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int[] nums = new int[24];
    for (int i = 1; i <= n; i++) {
        int index = scanner.nextInt();
        if (index < 1 || index > 23) {
            continue;
        }
        nums[index]++;
    }
    for (int i = 1; i < 13; i++) {
        for (int j = 1; j <= nums[i]; j++) {
            if (i * j > 24) {
                break;
            } else if (i * j == 24) {
                count++;
                break;
            } else {
                count(nums, i + 1, 24 - i * j);
            }
        }
    }
    System.out.println(count);
    scanner.close();
}

public static void count(int[] nums, int startIndex, int sum) {
    if (startIndex == nums.length) {
        return;
    }
    for (int j = 0; j <= nums[startIndex]; j++) {
        if (startIndex * j > sum) {
            break;
        } else if (startIndex * j == sum) {
            count++;
            break;
        } else {
            count(nums, startIndex + 1, sum - startIndex * j);
        }
    }
}

}

全部评论

相关推荐

11-09 01:22
已编辑
东南大学 Java
高级特工穿山甲:羡慕,我秋招有家企业在茶馆组织线下面试,约我过去“喝茶详谈”😢结果我去了发现原来是人家喝茶我看着
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务